【发布时间】:2017-01-08 09:44:58
【问题描述】:
我的应用中有Order 和Invoice 模型。 Order 具有属性 approved。当管理员在 Django-admin 中设置 approved=True 时,通知客户他们可以支付订单(Invoice)。
问题是Invoice 具有属性final_price,必须由管理员设置,然后管理员才能批准订单。
在 Django-admin 中,Invoice 对象内联到 Order 对象中。我想允许管理员设置final_price 属性并立即设置approved 订单。
所以我已经覆盖了Order 的clean(self) 方法。
def clean(self):
if self.approved and not self.invoice.final_price:
raise ValidationError(_("Invoice final price has to be set on approved order!"))
问题是如果管理员同时设置Invoice.final_price 和Order.approved,clean 方法会引发ValidationError,因为它不知道Invoice 即将被更改。
你对如何解决这个问题有什么建议吗?
【问题讨论】:
标签: python django django-models django-forms django-admin