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

前端 未结 5 935
长发绾君心
长发绾君心 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条回答
  •  猫巷女王i
    2021-02-02 18:06

    I think using a boolean 'is_active' flag is fine - you don't need to cascade the flag to related entries at the db level, you just need to keep referring to the status of the parent. This is what happens with contrib.auth's User model, remember - marking a user as not is_active doesn't prompt django to go through related models and magically try to deactivate records, rather you just keep checking the is_active attribute of the user corresponding to the related item.

    For instance if each user has many bookmarks, and you don't want an inactive user's bookmarks to be visible, just ensure that bookmark.user.is_active is true. There's unlikely to be a need for an is_active flag on the bookmark itself.

提交回复
热议问题