0
觸發定製我有ManyToManyField Wishlist
和Product
之間的自定義through
中介模式:m2m_changed不是 「通過」 的模式
class Product(models.Model):
name = models.CharField(max_length=255)
created = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ('-created',)
def __unicode__(self):
return self.name
class Wishlist(models.Model):
user = models.ForeignKey(User)
name = models.CharField(max_length=255)
created = models.DateTimeField(auto_now_add=True)
products = models.ManyToManyField(Product, through='WishlistProduct', null=True, blank=True)
class Meta:
ordering = ('-created',)
def __unicode__(self):
return self.name
class WishlistProduct(models.Model):
wishlist = models.ForeignKey(Wishlist)
product = models.ForeignKey(Product)
created = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ('-created',)
def __unicode__(self):
return u'%s in %s' % (self.product.name, self.wishlist.name)
並有m2m_changed
信號:
@receiver(m2m_changed, sender=Wishlist.products.through, dispatch_uid='m2m_changed_wishlist_products')
def m2m_changed_wishlist_products(sender, instance, action, *args, **kwargs):
print(sender)
print(action)
的m2m_changed
信號不工作,爲什麼?
但WishlistProduct的post_save
信號將被觸發。