Cascading Delete w/ Custom Model Delete Method

孤者浪人 提交于 2019-12-11 18:34:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!