Django Signal Receiver Function not accepting sender
You should specify the through
model of the ManyToManyField
of the model, so Post.liked_by.through
, not Post
: otherwise it is not clear on what ManyToManyField
you are subscribing. We thus define the handler as:
#core/signals.py
@receiver(m2m_changed, sender=Post.liked_by.through)
def likeNotification(sender,**kwargs):
# …
You can boost the efficiency to determine the number of likes with:
#socialuser/models.py
class Post(Authorable, Creatable, Model):
# …
def no_of_likes(self):
return self.liked_by.count()
then it will determine the number of likes at the database side, and thus reduce the bandwidth from the database to the Django/Python application layer.