Using Django: 1.10
Issue: Turn off pre_delete
signal and receiver sometimes
I want to use pre_delete
to delete other related records for a model.
And not call it at certain times even when I do want to delete the main model
What did I try?
I tried overriding the delete function in the main model PalletContent
like this:
def delete(self, *args, **kwargs):
self.auto_delete_line_item = False
if 'auto_delete_line_item' in kwargs:
self.auto_delete_line_item = kwargs['auto_delete_line_item']
del kwargs['auto_delete_line_item']
return super(PalletContent, self).delete(*args, **kwargs)
And then in the pre_delete
@receiver(pre_delete, sender=PalletContent)
def auto_delete_line_item(sender, instance, **kwargs):
if instance.auto_delete_line_item:
EntryFormHelper.remove_entry_form_line_item_from_pallet_content(
pallet_content=instance)
ExitFormHelper.remove_exit_form_line_item_from_pallet_content_if_any(
pallet_content=instance)
And then in the appropriate calls:
I expect this to activate the pre_delete
: pallet_content.delete(auto_delete_line_item=True)
I expect this not to activate the pre_delete
: pallet.pallet_contents.all().delete()
What I got back was:
File "/usr/../signals.py", line 31, in auto_delete_line_item
if instance.auto_delete_line_item:
AttributeError: 'PalletContent' object has no attribute 'auto_delete_line_item'