How can I easily mark records as deleted in Django models instead of actually deleting them?

前端 未结 5 912
长发绾君心
长发绾君心 2021-02-02 17:20

Instead of deleting records in my Django application, I want to just mark them as \"deleted\" and have them hidden from my active queries. My main reason to do this is to give

5条回答
  •  梦谈多话
    2021-02-02 18:10

    Django offers out of the box the exact mechanism you are looking for.

    You can change the manager that is used for access through related objects. If you new custom manager filters the object on a boolean field, the object flagged inactive won't show up in your requests.

    See here for more details : http://docs.djangoproject.com/en/dev/topics/db/managers/#using-managers-for-related-object-access

提交回复
热议问题