Skip to content

Instantly share code, notes, and snippets.

@Natim
Created April 13, 2020 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Natim/31f3fb9e7060b17146b13801ade90270 to your computer and use it in GitHub Desktop.
Save Natim/31f3fb9e7060b17146b13801ade90270 to your computer and use it in GitHub Desktop.
Register m2m changed event.
# Voir https://docs.djangoproject.com/fr/3.0/ref/signals/#m2m-changed pour plus d'infos
@receiver(m2m_changed, sender = VisiteMedicale.animaux.through)
def visite_medicale_save_action(sender, instance, **kwargs):
if reverse:
# Cas de popi.visite_medicale_set.remove(sterilisation_20200413)
animaux = [instance]
visite_medicale = model.objects.get(pk__in=pk_set)
else:
# Cas de sterilisation_20200413.animaux.add(Popi)
animaux = model.objects.filter(pk__in=pk_set).all()
visite_medicale = instance
type_visite = visite_medicale.type_visite
print("Type visite : " + type_visite + "\n")
sys.stdout.flush()
if type_visite in (TypeVisiteVetoChoice.STE.name, TypeVisiteVetoChoice.VAC.name):
sys.stdout.flush()
for animal in animaux:
print("Animal : " + animal.nom + "\n")
sys.stdout.flush()
if type_visite == TypeVisiteVetoChoice.STE.name:
animal.sterilise = OuiNonChoice.OUI.name
elif instance.type_visite == TypeVisiteVetoChoice.VAC.name:
animal.vaccine = OuiNonChoice.OUI.name
animal.date_dernier_vaccin = instance.date
animal.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment