Django reversion does not save revisions made in shell

此生再无相见时 提交于 2019-12-05 02:19:25
Dave

I wrote django-reversion, so I think I can shed some light on this issue.

A Version of a model is automatically saved when a model is saved, providing the following are true:

  1. The model is registered with django-reversion.
  2. The code block is marked up as being within a revision.

Point 1 can be achieved by either registering a model with VersionAdmin, or explicitly calling reversion.register() in your models.py file.

Point 2 can be achieved by using RevisionMiddleware, or the reversion.create_revision() decorator or context manager. Any admin views in VersionAdmin also save a revision.

So, if your shell is not creating Versions, then either point 1 or point 2 is not being met. Here's how to fix it:

  1. If you're using VersionAdmin, import the relevant admin module in your shell code to kick in the auto-registration. Alternatively, call reversion.register() in your models.py file.
  2. In your shell code, using the reversion.create_revision() context manager around your call to save.
with reversion.create_revision():
    s.save()

More about this sort of thing on the Low Level API wiki page:

http://django-reversion.readthedocs.org/en/latest/api.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!