Django: Creating a Mixin for Reusable Model Fields

无人久伴 提交于 2019-11-29 03:12:20

Abstract models still need to inherit from model.Model to work correctly:

class TrackingFieldsMixin(models.Model):

Also instead of your active BooleanField I would add a deleted_on DateTimeField so you can record when the record was deleted. You can then just add properties on the instance to see if it's active:

@property
def active(self):
    return self.deleted_on is None

and in queries and/or a custom manager:

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