I'm overwriting my model's method delete()
so that it executes an action before disappearing.
def delete(self, using=None, keep_parents=False):
print('ejecutando metodo delete')
producto_detallado = ProductoDetallado.objects.get(id=self.producto_detallado.id)
# El eliminado de producto venta incrementa el numero de unidades, a el numero de productos disponibles
producto_detallado.unidades_disponibles += self.unidades_vendidas
producto_detallado.save()
super(ProductoVenta, self).delete()
But deleting an object ProductoVenta
is not performing the expected action.
I put a prin()
so I can see in the console if it's running, but I don't actually have any messages there.
As I told you, the best thing you can do is use signals (signals). It's safer than overriding methods.
For your case, you would have to define a signal for when an instance of your model is deleted
ProductoVenta
:This function or handler will only be called when some instance of the model
ProductoVenta
is removed, either when you use the onedelete()
from the model or thedelete()
one from a queryset.