修订控制模型的好方法? - 姜戈

发布于 2024-10-26 03:07:06 字数 1437 浏览 2 评论 0原文

这是我的尝试:

示例#1

class RevisionControlledValue(models.Model):

    created = models.DateTimeField(auto_now_add=True)
    created_by = models.ForeignKey(User)

    value = models.TextField()

    class Meta:
        ordering = ('-created', )

class DjangoPony(models.Model):

    names = models.ManyToManyField(RevisionControlledValue, \
                related_name="fromname_djangopony_set")
    colors = models.ManyToManyField(RevisionControlledValue, \
                 related_name="fromcolor_djangopony_set")

    @property
    def name(self):
        return self.names.latest('created')

示例#2

class RevisionControlledValue(models.Model):

    created = models.DateTimeField(auto_now_add=True)
    created_by = models.ForeignKey(User)

    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')

    value = models.TextField()

    class Meta:
        ordering = ('-created', )

class DjangoPony(models.Model):

    @property
    def name(self):
        return self.revisioncontrolledvalue.latest('created')

两者对我来说都感觉有点不确定,尤其是 content_type ,我认为这应该是一个有利的选择。

  • 对于 content_type ,模型中不存在不同类型的修订控制字段的知识,如果我想要不同类型的字段怎么办?

  • 对于第一个多对多并不真正符合逻辑,因为它是一对多关系。


有什么建议或想法吗? :)

here is my attempt:

Example #1

class RevisionControlledValue(models.Model):

    created = models.DateTimeField(auto_now_add=True)
    created_by = models.ForeignKey(User)

    value = models.TextField()

    class Meta:
        ordering = ('-created', )

class DjangoPony(models.Model):

    names = models.ManyToManyField(RevisionControlledValue, \
                related_name="fromname_djangopony_set")
    colors = models.ManyToManyField(RevisionControlledValue, \
                 related_name="fromcolor_djangopony_set")

    @property
    def name(self):
        return self.names.latest('created')

Example #2

class RevisionControlledValue(models.Model):

    created = models.DateTimeField(auto_now_add=True)
    created_by = models.ForeignKey(User)

    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')

    value = models.TextField()

    class Meta:
        ordering = ('-created', )

class DjangoPony(models.Model):

    @property
    def name(self):
        return self.revisioncontrolledvalue.latest('created')

Both feel a bit iffy to me, especially the content_type one, which I would think should be favorable option.

  • With the content_type one, there is no knowledge within the model of the different types of revision controlled fields and what if I want different types of fields?

  • For the first one Many to Many does not really match the logic, as it is a One to Many relationship.


Any tips or ideas? :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

攒眉千度 2024-11-02 03:07:06

有一个用于 django-reversion 的应用程序。如果您确实只想对一个字段进行还原控制,请将其分成自己的表并建立一对一的关系。

There is app for that django-reversion. If you really only want to have reversion control on one field break it off into its own table and make a 1-to-1 relationship.

感性 2024-11-02 03:07:06

我认为您忘记了标题中的“字段”一词。 =)我想我会使用一种保持原始模型完整的方法。开头为:

class DjangoPony(models.Model):
    name = models.TextField()
    color = models.TextField()

class RevisionControlledValue(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    created_by = models.ForeignKey(User)
    value = models.TextField()
    field = models.CharField(max_length=20)

    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')

这假设您仅将其用于文本字段。为了使其真正美观,您可以创建一个新字段,例如 ReversionTextField,它会在保存模型时自动创建反转对象。

I think you forgot the word fields in your title. =) I guess I would use an approach that leaves the original model intact. Something starting with:

class DjangoPony(models.Model):
    name = models.TextField()
    color = models.TextField()

class RevisionControlledValue(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    created_by = models.ForeignKey(User)
    value = models.TextField()
    field = models.CharField(max_length=20)

    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')

This assumes you'll only be using it for TextFields. To make it really beautiful, you could create a new field, let's say ReversionTextField that automatically creates the reversion object when the model is saved.

烟凡古楼 2024-11-02 03:07:06

@Jason

你不需要创建新的模型......从回归文档:

保存字段的子集
如果您只想将字段的子集保存到修订版中,则可以为 register 方法指定 fields 参数。

reversion.register(YourModel, fields=["pk", "foo", "bar"])

回归经过了很好的测试,低级 API 可以让你做我想做的几乎所有事情。

@Jason

You don't need to create new models... from the reversion docs:

Saving a subset of fields
If you only want a subset of fields to be saved to a revision, you can specify a fields argument to the register method.

reversion.register(YourModel, fields=["pk", "foo", "bar"])

Reversion is pretty well tested and the low level api lets you do pretty much all I've ever wanted.

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