问题
I have a custom delete
method on my Model
that I make sure is called correctly when calling delete
on the QuerySet by using:
Custom delete method on queryset.
This does not seem to work when Django performs a cascading delete. In that case, the ORM calls _raw_delete
on a regular QuerySet
thereby bypassing my custom delete method.
How do I prevent that from happening?
The issue seems to be caused because this uses _base_manager
rather than _default_manager
:
def related_objects(self, related, objs):
return related.related_model._base_manager.using(self.using).filter(
**{"%s__in" % related.field.name: objs}
)
回答1:
It looks like I need to add this to the QuerySet
:
def _raw_delete(self, using):
self.delete()
_raw_delete.alters_data = True
and set use_for_related_fields = True
on the Manager
.
来源:https://stackoverflow.com/questions/29856011/cascading-delete-w-custom-model-delete-method