Override delete_selected() and update fields of child model
django, model
Solution
While this answer works, if you delete a single `obj` anywhere else in your code, the `delete_model` method will throw an error.
I recommend this:
actions = ['delete_selected']
def delete_selected(self, request, obj):
for o in obj.all():
Action.objects.filter(invoice=o).update(billed=False)
o.delete()
delete_selected.short_description = 'Ausgewählte Rechnungen löschen'
Just override the `delete_selected` action for the Change List admin page. This way you're not having to completely delete the action, override the `delete_model` method, and as a plus, you can still delete individual items from the Change `<object>` admin page. In the long run it's less code. :)
Problem
I'm trying to override the delete method for a model named Invoice. Model Invoice is referenced by ForeignKey in model Action. I want to update a BooleanField named billed in model Admin when it's Invoice is deleted the solution described in this post: How do I override delete() on a model and have it still work with related deletes that answer: https://stackoverflow.com/a/1539182 is not working for me like this in models.py: ``` def delete(self): Action.objects.filter(invoice=self).update(billed=False) # and tried 0 instead of False super(Invoice,self).delete() ``` also tried: ``` def delete(self): actions = Action.objects.filter(invoice=self) for action in actions: action.billed=False # and tried 0 instead of False action.save() super(Invoice,self).delete() ``` The ForeignKey Field in Model Action has a on_delete=models.SET_NULL to avoid to delete actions when the invoice is deleted. But I also need to set billed back to False. ``` invoice = models.ForeignKey( Invoice, verbose_name = 'Rechnung', null=True, blank=True,on_delete=models.SET_NULL) ``` I just read here https://code.djangoproject.com/ticket/10751 that in Admin the action delete selected could not be overridde with delete() I have to use delete_view() instead So I tried ``` def delete_selected(self, request, obj, extra_context=None): Action.objects.filter(invoice=self).update(billed=False) super(InvoiceAdmin, self).delete_view(request, obj, extra_context) ``` Also tried to use obj instead of self, but not the solution