Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I override delete method for an inline model within django?

I have a model 'B' that is linked to another model 'A' as an inline model, for use in my admin site. Now, whenever I delete an object of model 'B' associated with the corresponding object of model 'A' (via the admin site), I want to perform some more tasks at the backend. I was able to override the save function using a formset and then overriding the save_existing and save_new methods. How do I go about overriding the delete method for the inline admin model?

like image 606
Chetan Avatar asked Oct 31 '22 14:10

Chetan


2 Answers

I was able to get around by overriding the save() method for my model in the models.py itself.

like image 99
Chetan Avatar answered Nov 09 '22 09:11

Chetan


Use pre_delete or post_delete signals to execute code before/after model is deleted:

from django.db.models.signals import pre_delete
from django.dispatch import receiver
from myapp.models import MyModel


@receiver(pre_delete, sender=MyModel)
def my_handler(sender, **kwargs):
    ...
like image 38
Mariusz Jamro Avatar answered Nov 09 '22 10:11

Mariusz Jamro