django-reversion 恢复管理之外的多对多字段
我在我的项目中使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确,我认为您应该获得该版本的修订版;版本包含对象的数据,修订包含多个对象的版本。看一下:
具体来说,我认为你应该使用(未经测试):
注意,顺便说一句,回归应该知道它应该遵循关系。使用低级 API:
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:
Concretely, I think you should use (untested):
Note, btw, that reversions should know that it should follow relationships. Using the low level API:
我遇到了同样的问题,感谢@Webthusiast 的回答,我得到了我的工作代码。适应你的例子会是这样的。
导入:
注册您的模型:
然后您可以迭代:
此文档现在位于阅读文档中。请参阅“高级模型注册”部分低级 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:
Register your models:
And then you can iterate:
The documentation for this is now on Read the Docs. Refer to the 'Advanced model registration' section of the Low-level API page.