我需要在多对多关系中设置自定义保存和删除方法。
我尝试使用“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()
答案 0 :(得分:6)
您可以在包含关系的模型上使用django的m2m_changed
, pre_save/post_save
, pre_delete/post_delete signals
,并在那里执行相关逻辑。
答案 1 :(得分:0)
蒂米的回答绝对正确且有效。但是,对于您的特殊情况,我想知道,如果您不应该在视图中处理该逻辑,您在那里管理订单和订购商品并在那里强制重新计算,因为订单商品的添加,编辑和删除都属于一起并属于订单。