How to update a table when deleting a data in DJANGO

0

I'm working with Django. I'm making a shopping basket with Class Based Views. Everything works correctly, but I want that when removing an item from the basket (DeleteView) a function is executed to modify the quantity of another table. I just need to know how to execute a function when confirming the deletion of an element.

class CestaDeleteView(generic.DeleteView):
         model = Cesta
         success_url = reverse_lazy('cestas:cesta_list_view')

         @method_decorator(login_required)
         def dispatch(self, *args, **kwargs):
             return super(CestaDeleteView, self).dispatch(*args, **kwargs)
    
asked by David 26.08.2017 в 12:27
source

1 answer

0

Thanks to German's response, I managed to do what I wanted. It is done with signals (signals), Django has several types (before and after saving, before and after deleting, before and after migrating ...)

In the model

from django.db.models.signals import post_delete
from django.dispatch import receiver

''' A continuacion de la clase '''

@receiver(models.signals.post_delete, sender=Cesta)
def post_delete(sender, instance, **kwargs):
    instance.pedido.quantity += instance.quantity
    instance.pedido.save()
    
answered by 28.08.2017 / 11:29
source