我需要在多对多关系上设置自定义保存和删除方法。
我尝试使用“through”属性指定模型,但这使我的代码过于复杂并引入了一些问题。我不需要多对多模型上的任何额外字段,只需自定义保存和删除方法。
是否可以在不指定“通过”属性的情况下完成此操作?
这是代码:
class Order(BaseDate):
#lots of fields
relateds = models.ManyToManyField('RelatedProduct', verbose_name=_('related products'), blank=True, related_name='order_relateds', through='OrderRelateds')
# more fields
total = CurrencyField(verbose_name=_('total'))
def calculate_total(self):
cleanses = self.cleanse.taxed_price() * self.quantity
delivery = DELIVERY_PRICE if self.delivery == 'delivery' else 0
relateds = 0
for r in self.relateds.all():
relateds = relateds + float(r.taxed_price())
total = float(cleanses) + delivery + relateds
return total
def save(self, *args, **kwargs):
self.total = '%.2f' % self.calculate_total()
super(Order, self).save(*args, **kwargs)
class OrderRelateds(models.Model):
order = models.ForeignKey(Order)
relatedproduct = models.ForeignKey(RelatedProduct, verbose_name=_('related product'))
class Meta:
verbose_name = _('Related Product')
verbose_name_plural = _('Products Related to this Order')
def __unicode__(self):
return self.relatedproduct.__unicode__()
def save(self, *args, **kwargs):
super(OrderRelateds, self).save(*args, **kwargs)
self.order.save()
def delete(self, *args, **kwargs):
super(OrderRelateds, self).delete(*args, **kwargs)
self.order.save()
如果在订单中添加或删除任何相关产品(多对多项目),我需要触发订单总价的重新计算。
编辑:这是解决我问题的代码
from django.db.models.signals import m2m_changed
from django.dispatch import receiver
@receiver(m2m_changed, sender=Order.relateds.through)
def recalculate_total(sender, instance, action, **kwargs):
"""
Automatically recalculate total price of an order when a related product is added or removed
"""
if action == 'post_add':
instance.save()
if action == 'post_remove' or action == 'post_clear':
instance.save()