【问题标题】:Get all revisions with django-reversion使用 django-reversion 获取所有修订
【发布时间】:2015-04-17 16:21:45
【问题描述】:

我正在使用 django-reversion。如何打印 ListView 中每个模型的所有修订?

我试过了

class RevisionListView(ListView):
    model = reversion.revisions.Version
    template_name = "revision_list.html"

并在我的模板中打印查询集

{% for version in version_list %}
    {{ version }}
{% endfor %}

它似乎有效,但我不知道如何获取“原始对象”的链接(通过get_absolute_url)。似乎我得到了 object_id 和 content_type 但我不知道如何获取在 models.py 中定义的对象的绝对 url。

我可以打印修订号以及特定修订号在特定对象的修订号中吗?

我已经搜索过 SO,因为我认为其他人也有同样的问题,但我找不到任何东西。

【问题讨论】:

    标签: python django django-models django-reversion


    【解决方案1】:

    你有想过这个吗? Reversion 通过这样做在内部进行:

    def history_view(self, request, object_id, extra_context=None):
        """Renders the history view."""
        # check if user has change or add permissions for model
        if not self.has_change_permission(request):
            raise PermissionDenied
        object_id = unquote(object_id) # Underscores in primary key get quoted to "_5F"
        opts = self.model._meta
        action_list = [
            {
                "revision": version.revision,
                "url": reverse("%s:%s_%s_revision" % (self.admin_site.name, opts.app_label, opts.module_name), args=(quote(version.object_id), version.id)),
            }
            for version
            in self._order_version_queryset(self.revision_manager.get_for_object_reference(
                self.model,
                object_id,
            ).select_related("revision__user"))
        ]
        # Compile the context.
        context = {"action_list": action_list}
        context.update(extra_context or {})
        return super(VersionAdmin, self).history_view(request, object_id, context)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-27
      • 2011-12-23
      • 1970-01-01
      • 1970-01-01
      • 2019-02-22
      • 1970-01-01
      • 2018-01-30
      • 1970-01-01
      相关资源
      最近更新 更多