Reputation: 734
I am trying to save a ManyToMany field post save in a model. It's not possible to do so in the save() method as the object does not exist. So I tried putting the logic in a signal instead. My problem is: the signal seems to fire twice everytime I create a new FaculteAdmissions object. The main symptom is that the signal output is duplicated. Here is the code, it'll be a bit more obvious.
Model:
class FaculteAdmissions(models.Model):
trimestre = models.IntegerField(unique=True)
session = models.CharField(max_length=25)
annee = models.CharField(max_length=10)
date_lecture = models.DateField()
data = models.ManyToManyField(Admissions)
def save(self, *args, **kwargs):
"""
Exécuté avant la sauvegarde d'un objet de ce modèle.
Validera si dans la table Admissions il existe des données pour ce trimestre.
"""
# valide l'unicité
if FaculteAdmissions.objects.filter(trimestre=self.trimestre).exists():
raise ValidationError("Un objet ayant ces valeurs existe déjà")
else:
# valide les données
q = Admissions.objects.filter(trimestre=self.trimestre,
niveau=constantes.NIVEAUX_LECTURE_BD[2])
if not q.exists():
raise ValidationError("Données introuvables dans la table Admissions")
# OK!
super(FaculteAdmissions, self).save(*args, **kwargs)
# Envoie signal pour mettre à jour les données
post_save.send(sender=self.__class__, instance=self, created=True)
def __str__(self):
return "Faculté - Admissions"
associated signal:
def creation_faculte_admissions(sender, instance, created, **kwargs):
if created:
# valide les données
q = Admissions.objects.filter(trimestre=instance.trimestre,
niveau=constantes.NIVEAUX_LECTURE_BD[2])
instance.data = q
print("Données ajouté pour " + str(instance) + str(instance.trimestre))
and the signal connect:
post_save.connect(creation_faculte_admissions, sender=FaculteAdmissions, dispatch_uid="faculte_admissions")
Upvotes: 0
Views: 265
Reputation: 734
As @karthikr mentionned, the correct signal to use is m2m_changed
.
Simply switching to that signal from post_save
did the trick.
Upvotes: 1