想要在UserProfile中调用follow()
函数时使用信号。我写的信号与其他型号一起使用(保存时)。
class UserProfile(UserenaBaseProfile):
user = models.OneToOneField(User,unique=True,verbose_name=_('user'))
location = models.CharField(_('Location'), max_length=255, blank=True, default='')
following = models.ManyToManyField(User, verbose_name=_('following'), related_name='followers', blank=True, null=True)
def follow(self, user):
self.following.add(user)
def unfollow(self, user):
self.following.remove(user)
信号
def follow_action(sender, instance, created, action_object=None, **kwargs):
action.send(instance.user, verb='follows', target=instance.content_object)
post_save.connect(follow_action, sender=UserProfile)
当UserProfile
模型保存时,此信号有效。我希望在follow()
函数执行时调用signal。你能帮忙吗?任何的想法?非常感谢
更新:
def follows_action(sender, instance, created, action_object=None, **kwargs):
action.send(instance.user, verb='follows', target=instance.content_object)
m2m_changed.connect(follows_action, sender=UserProfile.following.through)
它不起作用。我错过了什么吗?