0

I have a custom through intermediary model for ManyToManyField between Wishlist and 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)

And a m2m_changed signal:

@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)

The m2m_changed signal is not working, why?

But the post_save signal for WishlistProduct will be trigger.

Vinta
  • 387
  • 4
  • 10

1 Answers1

0

You signal is probably being garbage collected. Pass in weak=False to connect method.

https://docs.djangoproject.com/en/dev/topics/signals/#listening-to-signals

jorilallo
  • 788
  • 1
  • 8
  • 23