django-oscar的物流状态pending修改以及分析源码解决报错: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变量.

上述就是关于这个问题的排查思路

然后问题来了,我们会发现在用户的历史订单中,

物流状态不显示,怎么办呢?

首先确保:

templates/oscar/customer/order/order_detail.html

中有下面的代码:

    {% if order.status %}

    <h2>{% trans 'Status' %}</h2>
    {{order.status}}

    <hr>
    {% endif %}
发布了758 篇原创文章 · 获赞 343 · 访问量 162万+

猜你喜欢

转载自blog.csdn.net/appleyuchi/article/details/104759167
今日推荐