1

想要follow()在 UserProfile 中调用函数时使用信号。我已经编写了可与其他模型一起使用的信号(保存时)。

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()函数执行时调用信号。你能帮忙吗?任何的想法?多谢

更新:

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)

这没用。我错过了什么吗?

4

1 回答 1

2

要将回调连接到多对多关系的更改,请使用m2m_changed 信号

于 2012-05-15T17:23:45.047 回答