Django:为可重用模型字段创建 Mixin

发布于 2024-11-07 22:52:39 字数 1081 浏览 2 评论 0原文

我想将一些字段添加到项目中的大多数模型中。例如,这些字段是“跟踪字段”,例如创建日期、更新日期和“活动”标志。我正在尝试创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月野兔 2024-11-14 22:52:39

抽象模型仍然需要继承 model.Model 才能正常工作:

class TrackingFieldsMixin(models.Model):

另外,我会添加一个 deleted_on< 来代替 active BooleanField /code> DateTimeField 这样您就可以记录删除记录的时间。然后,您可以在实例上添加属性以查看它是否处于活动状态:

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

并在查询和/或 自定义管理器

Blog.objects.filter(deleted_on__isnull=True)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文