听取新的多对一关系的信号是什么?

时间:2017-01-24 13:31:15

标签: django django-models many-to-one django-signals

from django.db import models

class Reporter(models.Model):
    pass

class Article(models.Model):
    reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE, null=True)

使用上述模型,如果我想知道何时将文章添加到记者中,我应该听哪个信号?

>>> new_article = Article()
>>> new_reporter = Reporter()
>>> new_reporter.article_set.add(new_article)

我已经尝试了m2m_changedpost_save,但都没有效果

from django.db.models.signals import m2m_changed, post_save
from django.dispatch import receiver

@receiver(m2m_changed)
def m2m_add(sender, instance, **kwargs):
    print "m2m_add triggered!"

@receiver(post_save)
def post_save_add(sender, instance, **kwargs):
    print "post_save_add triggered!"

1 个答案:

答案 0 :(得分:1)

连接到post_save的{​​{1}},但您必须call add with bulk=False

Article