Django:为可重用模型字段创建 Mixin
我想将一些字段添加到项目中的大多数模型中。例如,这些字段是“跟踪字段”,例如创建日期、更新日期和“活动”标志。我正在尝试创建一个 Mixin,我可以将其添加到每个模型类中,这将允许我通过多重继承添加这些额外的字段。但是,当创建对象实例时,通过 Mixin 添加的模型字段似乎显示为对象的方法而不是数据库字段。
In [18]: Blog.objects.all()[0].created
Out[18]: <django.db.models.fields.DateTimeField object at 0x10190efd0>
这是我的模型的样子:
from django.db import models
class Blog(models.Model, TrackingFieldMixin):
name = models.CharField(max_length=64)
type = models....
class TrackingFieldsMixin():
active = models.BooleanField(default=True,
help_text=_('Indicates whether or not this object has been deleted.'))
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
所以这似乎不起作用。有谁知道我如何为类似于上面的常见模型字段创建可重用的混合?这种方法有缺陷吗?
谢谢你的帮助, Joe
更新:请注意,我计划在 MPTT 模型中使用 mixin 的一些模型,因此我不能简单地将 TrackingFieldMixin mixin 基类并仅从它继承。
class Post(MPTTModel, TrackingFieldMixin):
post_name = models....
post_type = models...
I've got a few fields that I want to add to most every model in my project. For example, these fields are "tracking fields" such as a created date, an update date, and an "active" flag. I'm attempting to create a Mixin that I could add to each model class that would allow me to add these extra fields via multiple inheritance. However, when an object instance is created, it appears that my model fields that were added via the Mixin show up as methods of the object rather than database fields.
In [18]: Blog.objects.all()[0].created
Out[18]: <django.db.models.fields.DateTimeField object at 0x10190efd0>
Here is what my models look like:
from django.db import models
class Blog(models.Model, TrackingFieldMixin):
name = models.CharField(max_length=64)
type = models....
class TrackingFieldsMixin():
active = models.BooleanField(default=True,
help_text=_('Indicates whether or not this object has been deleted.'))
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
So this doesn't appear to work. Does anyone know how I am able to create a reusable mixin for common model fields similar to above? Is there a flaw in this approach?
Thanks for you help,
Joe
Update: Note that some of my models that I plan to use the mixin in use the MPTT model, so I can't simply make my TrackingFieldMixin mixin the base class and inherit only from it.
class Post(MPTTModel, TrackingFieldMixin):
post_name = models....
post_type = models...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抽象模型仍然需要继承
model.Model
才能正常工作:另外,我会添加一个
deleted_on< 来代替
active
BooleanField
/code>DateTimeField
这样您就可以记录删除记录的时间。然后,您可以在实例上添加属性以查看它是否处于活动状态:并在查询和/或 自定义管理器:
Abstract models still need to inherit from
model.Model
to work correctly:Also instead of your
active
BooleanField
I would add adeleted_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:and in queries and/or a custom manager: