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