django-oscar的物流狀態修改以及分析源碼解決報錯:The new status 'xxx' is not valid for this order

先說物流狀態的修改:

django-oscar自帶的物流狀態是四個:

OSCAR_INITIAL_ORDER_STATUS = 'Pending'
OSCAR_INITIAL_LINE_STATUS = 'Pending'
OSCAR_ORDER_STATUS_PIPELINE = {
    'Pending': ('Being processed', 'Cancelled',),
    'Being processed': ('Processed', 'Cancelled',),
    'Cancelled': (),
}

 

我們改爲:

OSCAR_INITIAL_ORDER_STATUS = "已經拍下,等待支付"
OSCAR_INITIAL_LINE_STATUS = "Pending"
#這個是供貨商的狀態,不需要理會

OSCAR_ORDER_STATUS_PIPELINE = {
    "已經拍下,等待支付":("買家已經支付,等待賣家發貨","取消訂單"),
    "買家已經支付,等待賣家發貨":("賣家已經發貨","取消訂單"),#這裏需要引入物流詳情
    "賣家已經發貨": ("物流派件中,等待買家取件", "Cancelled"),
    "物流派件中,等待買家取件": ("訂單交易成功", "Cancelled"),
    "訂單交易成功":(),
    "訂單取消":(),
}

 

好了,有些同學想要定製修改上面的訂單狀態,但是一不小心在使用的時候就出現了:

The new status 'xxx' is not valid for this order

先從settings.py中開始檢查:

①查看標點是不是都是統一的圓角或者半角

②引號請統一,別一會兒單引號一會兒雙引號.

③不能跨key-value修改訂單狀態,上面我們看到,總共6個key-value對,

你可以在dashboard中對某個訂單從"已經拍下,等待支付"改成"買家已經支付,等待賣家發貨",

但是你不能在dashboard中對某個訂單從"已經拍下,等待支付"直接改成"物流派件中"

#------------------------------------

如果上面檢查過後還是報這個錯,

那麼我們來查看源碼:

上面的③受源碼/home/appleyuchi/.virtualenvs/python3.7/lib/python3.7/site-packages/oscar/apps/dashboard/views.py

中函數決定

    def change_order_status(self, request, order):
        # This method is pretty similar to what
        # OrderDetailView.change_order_status does. Ripe for refactoring.
        new_status = request.POST['new_status'].strip()
        if not new_status:
            messages.error(request, _("The new status '%s' is not valid")
                           % new_status)
        elif new_status not in order.available_statuses():
            print("new_status=",new_status)
            print("order.available_statuses=",order.available_statuses())
            messages.error(request, _("The new status '%s' is not valid for"
                                      " this order") % new_status)
        else:
            handler = EventHandler(request.user)
            old_status = order.status
            try:
                handler.handle_order_status_change(order, new_status)
            except PaymentError as e:
                messages.error(request, _("Unable to change order status due"
                                          " to payment error: %s") % e)
            else:
                msg = _("Order status changed from '%(old_status)s' to"
                        " '%(new_status)s'") % {'old_status': old_status,
                                                'new_status': new_status}
                messages.info(request, msg)
                order.notes.create(
                    user=request.user, message=msg, note_type=OrderNote.SYSTEM)

當訂單狀態選擇不對或者"兩個訂單狀態不相鄰"(注意看key-value對)就會報錯.

上面的函數調用了:

/home/appleyuchi/.virtualenvs/python3.7/lib/python3.7/site-packages/oscar/apps/order/abstract_models.py

    def available_statuses(self):
        """
        Return all possible statuses that this order can move to
        """
        print("函數內self.pipeline=",self.pipeline)
        print("返回的是:",self.pipeline.get(self.status, ()))
        print("self_status=",self.status)

        return self.pipeline.get(self.status, ())

而這個abstract_models.py會去讀取OSCAR_ORDER_STATUS_PIPELINE變量.

上述就是關於這個問題的排查思路

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章