简体   繁体   English

如何在django reversion中存储父指针和数据?

[英]How to store parent pointers and data in a django reversion?

I'm having some trouble with some code for a django-reversion system I have set up. 对于我设置的django-reversion系统的代码,我遇到了一些问题。

I have a django model ObjectClass that inherits from a non-abstract model _concept . 我有一个django模型ObjectClass ,它继承自非抽象模型_concept Long story, very unchangeable now. 长话故事,现在非常不可改变。 Makes sense in the rest of the context of the code. 在代码的其余部分中有意义。

On the class _concept I have mandatory ForeignKey field workgroup off to another model. 在类_concept我将强制的ForeignKey字段workgroup关闭到另一个模型。

I have registered ObjectClass with Django reversion like so: 我已经使用Django reversion注册了ObjectClass ,如下所示:

reversion.unregister(ObjectClass)
reversion.register(ObjectClass,follow=['_concept_ptr'])

All old versions save and can be compared using 'django-reversion-compare'. 所有旧版本都保存并可以使用'django-reversion-compare'进行比较。 Unfortunately when I then click on any old version to view the historical version I get the error: 不幸的是,当我点击任何旧版本查看历史版本时,我收到错误:

RevertError at /module.py
Could not revert revision, due to database integrity errors.

After some digging in the django-reversion code, I've done some fiddling and found that the error is coming up thus: 在挖掘了django-reversion代码后,我做了一些小事,发现错误正在出现:

RelatedObjectDoesNotExist at /module.py
_concept has no workgroup.

Now I've inspected the stored versions in the database and found a few things: 现在我已经检查了数据库中存储的版本,发现了一些东西:

  1. Any given historical reversion.models.version of a _concept has a workgroup in the serialized_data field (which is expected). _concept的任何给定的历史reversion.models.versionserialized_data字段中都有一个工作组(这是预期的)。
  2. Any given historical reversion.models.version of a ObjectClass does not have any of the parent information in the serialized_data field (which is expected). ObjectClass任何给定历史reversion.models.version 都没有 serialized_data字段中的任何父信息(这是预期的)。
  3. Any given historical reversion.models.version of an ObjectClass does not have any a _concept_ptr in the serialized_data field (which is not expected). ObjectClass任何给定的历史reversion.models.versionserialized_data字段中没有任何_concept_ptr (这是期望的)。

I'd suspect that django-reversion may have issues with fields that start with an underscore, however I have other fields that start with an underscore. 我怀疑django-reversion可能存在以下划线开头的字段的问题, 但是我有其他以下划线开头的字段。

So I'm at a loss here. 所以我在这里不知所措。 Is there a way to have this a model setup like this work? 有没有办法让这个模型设置像这样的工作?


edit: 编辑:

After more checking it seems that the has no workgroup exception was from a Haystack call, which was alerting me to the fact that reversion is ignoring the workgroup for some reason. 在进行了更多检查之后,似乎has no workgroup异常来自Haystack调用,这提醒我,由于某种原因, reversion忽略了工作组。

I checked the database and this is whats in being serialized for an item (newlines added for readability): 我检查了数据库,这是为一个项目序列化的内容(为了可读性添加了新行):

In [28]: myobj.serialized_data
Out[28]: u'[{"fields": {
               "definition": "<p>A code for sex.</p>\\r\\n",
               "_is_locked": false, 
               "workgroup": 3, 
               "created": "2015-12-27T07:45:10.409Z", 
               "modified": "2015-12-27T08:38:26.989Z", 
               "readyToReview": false, 
               "_is_public": false, 
               "name": "Sex Code"
             }, 
             "model": "aristotle_mdr._concept", "pk": 30}]'

edit 2: 编辑2:

After disabling the haystack indexers everything works fine now, the issue is the Haystack signals are called when django-reversion tries to save the items to check consistency - then django calls the haystack post_save signals which try to update the index with incomplete data. 禁用干草堆索引器之后一切正常,问题是当django-reversion尝试保存项目以检查一致性时调用post_save信号 - 然后django调用干草堆post_save信号,试图用不完整的数据更新索引。

Still no solution yet. 还没有解决方案。 What I need in my haystack handler is either a way to determine if I'm inside a revisions transaction, or a way to prevent reversion from letting those signals fire. 我在干草堆处理程序中需要的是确定我是否在修订事务中的方法,或者是一种防止返回让这些信号触发的方法。 The latter is probably a better long term goal as I suspect that just by looking at revisions it is updating the Haystack index. 后者可能是一个更好的长期目标,因为我怀疑只是通过查看修订版它正在更新干草堆指数。

So you have come to a conclusion prevent reversion from letting those signals fire 所以你得出了一个结论,以prevent reversion from letting those signals fire

https://docs.djangoproject.com/en/dev/topics/signals/#disconnecting-signals https://docs.djangoproject.com/en/dev/topics/signals/#disconnecting-signals

django-reversion itself uses Signal.connect and disconnect itself. django-reversion本身使用Signal.connect并断开连接。
https://github.com/etianen/django-reversion/blob/b2f5f3362054b2b72a95bee1ed0dfe2dd2301cda/src/reversion/revisions.py https://github.com/etianen/django-reversion/blob/b2f5f3362054b2b72a95bee1ed0dfe2dd2301cda/src/reversion/revisions.py

I see you have few options. 我看你几乎没有选择。

  1. find a good place to do disconnect/connect (I guess you would need to override reversion, but I don't know much about it) 找个disconnect/connect的好地方(我想你需要覆盖reversion,但我不太了解它)
  2. (override reversion) exclude from registering certain signals such as haystack. (覆盖reversion)排除注册某些信号,如haystack。
  3. (override haystack, reversion) set a flag on the being-saved object, and check the flag in the haystack signal to return right away. (覆盖haystack,reversion)在正在保存的对象上设置一个标志,并检查haystack信号中的标志以立即返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM