user514310
user514310

Reputation:

use signal on m2m_changed

Want to use signal when follow() function called in UserProfile. I've written signal which works with other models (on save).

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)

signal

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)

this signal works when UserProfile model saved. I want to call signal when follow()function execute. will you please help? any idea? thanks alot

UPDATE:

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)

It does not work. Am i missing something?

Upvotes: 1

Views: 511

Answers (1)

jpic
jpic

Reputation: 33420

To connect a callback to a change of a many to many relation, use m2m_changed signal.

Upvotes: 2

Related Questions