django-reversion 恢复管理之外的多对多字段

发布于 2024-11-18 10:11:10 字数 630 浏览 11 评论 0原文

我在我的项目中使用 django-reversion。 除了一件事之外,它的效果很好: 我无法获取 ManyToMany 字段的早期版本。但在 django admin 中它是有效的,而不是在我的代码中。 为了获得以前的版本,我使用以下代码:

vprod = Version.objects.get_for_date(product, ondate).get_object_version().object

并且它可以工作,除了 m2m 字段 其中“product”是 Product 类的对象,

class Product(models.Model):
    name = models.CharField(max_length=255)
    elements = models.ManyToManyField(Sku)

class Sku(models.Model):
    name = models.CharField(max_length=255, verbose_name="SKU Name")

我可以获得 vprod.name 并返回我需要的内容,但是当我尝试 vprod.elements.all() 时它返回仅列出当前(最后)版本,即使元素数量发生变化。

I am using django-reversion in my project.
And it works good except one thing:
I can't get previous versions of ManyToMany fields. But in django admin it is works, not in my code.
To get previous version I use following code:

vprod = Version.objects.get_for_date(product, ondate).get_object_version().object

and it works except m2m field
where 'product' is object of Product class,

class Product(models.Model):
    name = models.CharField(max_length=255)
    elements = models.ManyToManyField(Sku)

class Sku(models.Model):
    name = models.CharField(max_length=255, verbose_name="SKU Name")

I can get vprod.name and it returns what I need, but when I try vprod.elements.all() it returns list only the current (last) version, even if the number of elements changed.

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

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

发布评论

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

评论(2

纸短情长 2024-11-25 10:11:10

如果我理解正确,我认为您应该获得该版本的修订版;版本包含对象的数据,修订包含多个对象的版本。看一下:

some_version.revision.version_set.all()

具体来说,我认为你应该使用(未经测试):

[
    v for v in Version.objects.get_for_date(product, ondate) \
        .revision.version_set.all()
    if version.content_type == ContentType.objects.get_for_model(Sku)
]

注意,顺便说一句,回归应该知道它应该遵循关系。使用低级 API

reversion.register(YourModel, follow=["your_foreign_key_field"])

If I understand it correctly, I think you should get the revision for the version; the version contains the data of the object, the revision contains versions for multiple objects. Have a look at:

some_version.revision.version_set.all()

Concretely, I think you should use (untested):

[
    v for v in Version.objects.get_for_date(product, ondate) \
        .revision.version_set.all()
    if version.content_type == ContentType.objects.get_for_model(Sku)
]

Note, btw, that reversions should know that it should follow relationships. Using the low level API:

reversion.register(YourModel, follow=["your_foreign_key_field"])
衣神在巴黎 2024-11-25 10:11:10

我遇到了同样的问题,感谢@Webthusiast 的回答,我得到了我的工作代码。适应你的例子会是这样的。

导入:

from django.contrib.contenttypes.models import ContentType
import reversion

注册您的模型:

reversion.register(Sku)
reversion.register(Product, follow=['elements'])

然后您可以迭代:

object = Product.objects.get(some_id)
versions = reversion.get_for_object(self.object)
for version in versions:
    elements = [v.object_version.object \
        for v in version.revision.version_set.all() \
        if v.content_type == ContentType.objects.get_for_model(Product)]

此文档现在位于阅读文档中。请参阅“高级模型注册”部分低级 API 页面的。

I had the same issue and thanks to @Webthusiast's answer I got my working code. Adapting to your example would be something like this.

Imports:

from django.contrib.contenttypes.models import ContentType
import reversion

Register your models:

reversion.register(Sku)
reversion.register(Product, follow=['elements'])

And then you can iterate:

object = Product.objects.get(some_id)
versions = reversion.get_for_object(self.object)
for version in versions:
    elements = [v.object_version.object \
        for v in version.revision.version_set.all() \
        if v.content_type == ContentType.objects.get_for_model(Product)]

The documentation for this is now on Read the Docs. Refer to the 'Advanced model registration' section of the Low-level API page.

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