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.statustry: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 %}?
?
?
總結
以上是生活随笔為你收集整理的django-oscar的物流状态pending修改以及分析源码解决报错:The new status 'xxx' is not valid for this order的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenAI 开 300 万 + 年薪招
- 下一篇: 天眼怎么查企业信息