Project in Django: Growing and validating with django signals
We begin with a giant form and need to create many objects from different models while discussing the first and basic approach to them arrive in Django Signals and the project can escalate and be maintainable
to create a giant form because automatizations Need to create models on cascade Also need to validate on cascade Need to be ATOMIC, easy rollbacks The user must know what’s the error
= form.cleaned_data['attributes_for_adm'] attributes_for_com = form.cleaned_data['attributes_for_com'] attributes_for_mng = form.cleaned_data['attributes_for_mng'] if attributes_for_noc.has_correct_stuff: if attributes_for_noc.name.contains("Super"): noc = NOC.objects.create(attributes_for_noc) if attributes_for_adm.has_correct_stuff: if attributes_for_adm.name.contains("Super"): adm = ADM.objects.create(attributes_for_adm) if attributes_for_com.has_correct_stuff: if attributes_for_com.name.contains("Super"): com = COM.objects.create(attributes_for_com) if attributes_for_mng.has_correct_stuff: if attributes_for_mng.name.contains("Super"): mng = MNG.objects.create(attributes_for_mng) if not (noc and adm and com and mng): raise ValidationError("failed") messages.success(self.request, "created") return redirect(self.request.META.get('PATH_INFO')) except ValidationError as e: messages.error(self.request, e.messages) return redirect(self.request.META.get('PATH_INFO'))
want to customize A classic use-case is if you want something to happen whenever you save an object It’s important to remember to call the superclass method super().save(*args, **kwargs) to ensure that the object still gets saved into the database Unfortunately, there isn’t a workaround when creating or updating objects in bulk, since none of save(), pre_save, and post_save are called. https://docs.djangoproject.com/en/2.2/topics/db/models/#overriding-model-methods
new entry @receiver(post_save, sender=ModelCrazy) def create_crazy_stuff(sender, instance, **kwargs): if kwargs['created'] and not kwargs['raw']: if instance.super_crazy: create_crazy_stuff(instance) else: create_good_stuff(instance) # Update atributes if object is old entry @receiver(post_save, sender=ModelCrazy) def update_stuff(sender, instance, update_fields, **kwargs): if not kwargs['created'] and not kwargs['raw']: if instance and instance.can_update_stuff: instance.update_stuff()
◦ update fields @receiver(post_save, sender=SomeModel) def create_another_model(sender, instance, **kwargs): if kwargs['created'] and not kwargs['raw']: if instance.create_another_model: create_all_instance_of_model(instance) # Just make a curl whenever ModelNormal is saved @receiver(post_save, sender=ModelNormal) def call_method_to_stuff(sender, instance, **kwargs): curl_to_check_madness(instance)
a series of instances if necessary same model or another # Delete all children from ModelCrazy @receiver(post_delete, sender=ModelCrazy) def delete_orphan_from_crazy_always(sender, instance, **kwargs): delete_orphans(instance)