我正在 django 开发电子商务。问题是关于 Order 和 RowOrder 计算:
class Order(Model):
STATUS = {
'NPAI': 'Not paid',
'PAID': 'Paid',
'SHIP': 'Shipped',
}
status = CharField(max_length=4, choices=STATUS.items(), editable=False, default='NPAI')
cod = CharField(max_length=30, unique=True, db_index=True, editable=False)
total_price = DecimalField(max_digits=4, decimal_places=2, default=Decimal('0.00'), editable=False)
vat = DecimalField(max_digits=4, decimal_places=2, default=Decimal('0.00'), editable=False)
[...]
class RowOrder(Model):
article = ForeignKey(Article)
order = ForeignKey(Order)
[...]
def save(self, *args, **kwargs):
# update order
with transaction.commit_on_success():
order_Locked = Order.objects.select_for_update().get(id=self.order.id)
order_Locked.total_price += self.price
order_Locked.vat_price += self.price - self.price/(1 + order_Locked.vat/100)
order_Locked.save()
如您所见,每次保存roworder时,我都会更新order 的值。这很好用,但现在我必须在创建订单时发送订单确认电子邮件,但如果我在 Order.save() 中发送电子邮件:
def save(self, *args, **kwargs):
if self.status == 'NPAI':
super(Order, self).save(*args, **kwargs)
send_order_confirm(self)
那么,正确地,那个时候的 self.roworder_set 是空的。我选择了错误的方法,或者实际上我可以做些什么来实现我的目标?太感谢了。