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_changed
和post_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!"