【发布时间】:2016-05-04 23:52:03
【问题描述】:
我的 post_save 函数有问题。该函数已正确触发,但实例不包含插入的值。我用ipdb检查了函数,没有错。只是 ManyToManyField 是空的。
代码:
@receiver(post_save, sender=Supplier)
def set_generic_locations(sender, instance, **kwargs):
""" Set the generic locations for the NEW created supplier.
"""
created = kwargs.get('created')
if created:
glocations = LocationAddress.get_generic_locations()
for location in glocations:
instance.locations.add(location)
instance.save()
实例中使用的字段:
locations = models.ManyToManyField(LocationAddress, blank=True)
我不明白为什么,但位置总是空的。
我使用 django 1.8.8
更新
问题是 django 管理员。我在这里找到了解释:http://timonweb.com/posts/many-to-many-field-save-method-and-the-django-admin/
django admin 中解决问题的代码
def save_related(self, request, form, formsets, change):
super(SupplierAdmin, self).save_related(request, form, formsets, change)
form.instance.set_generic_locations()
【问题讨论】:
-
对不起,卡里姆,set_generic_locations 函数已作为方法添加到模型供应商中?感谢您的宝贵时间
-
@nachopro: 不,它只是模型模块底部的一个函数
标签: django django-signals