と の間に ManyToManyFieldのカスタムthrough
中間モデルがWishlist
ありProduct
ます:
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
なぜですか?
しかし、post_save
WishlistProduct のシグナルがトリガーになります。