0

我在和through之间有一个用于 ManyToManyField 的自定义中介模型:WishlistProduct

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_saveWishlistProduct 的信号将被触发。

4

1 回答 1

0

您的信号可能正在被垃圾收集。传入weak=False连接方法。

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

于 2013-01-02T14:58:55.297 回答