简体   繁体   English

django-reversion 控制对象的先前版本

[英]Previous version of a django-reversion controlled object

I'm using what the django-reversion documents call the low-level API in order to access the reversion history in my own code, apart from the admin.除了管理员之外,我正在使用 django-reversion 文档调用的低级 API来访问我自己代码中的还原历史记录。 To store metadata, I've extended the Revision model by setting up my own model including a OneToOneField(Revision) .为了存储元数据,我通过设置自己的模型(包括OneToOneField(Revision)扩展了Revision模型。 So far, so good.到现在为止还挺好。

But given that reference to a Revision , how can I access the revision directly before it?但是考虑到对Revision引用,我如何直接之前访问修订版? For example, to generate a list of changes between this revision and the previous one, is there any more efficient method than calling back to reversion.get_for_object and searching the list for the version I'm looking for?例如,要生成此修订版和上一个修订版之间的更改列表,有没有比回调reversion.get_for_object并在列表中搜索我要查找的版本更有效的方法?

The Revision object has a date_created attribute, so you could write a query to select the single most recent revision for your object prior to the given revision's date_created . Revision 对象有一个date_created属性,因此您可以编写一个查询来为您的对象在给定修订的date_created之前选择一个最新的修订。 I'd basically copy the implementation of the low-level API's get_for_date function, with one change -- use "date_created__lt" instead of "__lte" :我基本上复制了低级 API 的get_for_date函数的实现,有一个变化——使用"date_created__lt"而不是"__lte"

def get_previous(object, date):
    """Returns the latest version of an object prior to the given date."""
    versions = reversion.get_for_object(object)
    versions = versions.filter(revision__date_created__lt=date)
    try:
        version = versions[0]
    except IndexError:
        raise Version.DoesNotExist
    else:
        return version

版本对象有一个修订属性,它有两个方法,“get_next_by_date_created”和“get_previous_by_date_created”,你可以用这两种方法来遍历版本历史。

version.revision.get_previous_by_date_created()

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

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