2

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}
  )
Community
  • 1
  • 1
Alex Rothberg
  • 10,243
  • 13
  • 60
  • 120

1 Answers1

2

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.

Alex Rothberg
  • 10,243
  • 13
  • 60
  • 120