Search code examples
djangopython-3.xdjango-modelsdjango-signals

After Model and many to many fields saved signal django


I have models like

class Genre(models.Model):
      name = models.CharField(max_length=50)

class Cast(models.Model):
      name = models.CharField(max_length=120, null=False, blank=False)

class movie:
      name = models.CharField(max_length=120, null=False, blank=False)
      genre = models.ManyToManyField(Genre)
      cast = models.ManyToManyField(Cast, null=True, blank=True)

I want to send notification to clients after a movie saved so i used post_save signal and because of my m2m relationships it didn't work and after that i used m2m_changed and now everytime i make a change on movie genres or movie cast they will be notified! i want them to be notified just for the first time the movie submits and i need the genres too!

I mean the problem with the post_save signal was, it happens before genre and cast objects submit so i didn't have access to them.


Solution

  • I had to add a BooleanField to movie model named notified, and after first time i check and and everytime before sending i check if it have been checked and thanks to @Mayk, he was part of the idea