3

我想在我的项目中使用django reversion应用程序。我有点困惑。如何在模板中显示更改列表(field_dict.items())?

模型.py

@reversion.register()
class Function(models.Model):
    ***FIELDS***

视图.py

def function_revisions(request, function_code):
    function = get_object_or_404(Function, pk=function_code)
    versions = Version.objects.get_for_object(Function)
    context = {
        'function': function,
        'versions': versions,
    }
    return render(request, 'project/function_revisions.html', context)

function_revisions.html

{% for version in versions %}
     <p>Version: {{ forloop.counter }}.</p>
     <p>Revision by {{ version.revision.user }}.</p>
     <p>Date of revision : {{ version.revision.date_created }}.</p>

     {% for field_name, field_value in version.field_dict.items() %} <-- ???
         <p>Field </p>{{ field_name }} had the value {{ field_value }}<p>
     {% endfor %}
{% endfor %}

错误

Traceback (most recent call last):
  File "C:\Users\Nurzhan\PycharmProjects\RMS\project\views.py", line 231, in function_revisions
    versions = Version.objects.get_for_object(Function)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\reversion\models.py", line 118, in get_for_object
    return self.get_for_object_reference(obj.__class__, obj.pk, model_db=model_db)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\reversion\models.py", line 113, in get_for_object_reference
    return self.get_for_model(model, model_db=model_db).filter(
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\reversion\models.py", line 106, in get_for_model
    content_type = _get_content_type(model, self.db)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\reversion\revisions.py", line 402, in _get_content_type
    version_options = _get_options(model)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\reversion\revisions.py", line 388, in _get_options
    _assert_registered(model)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\reversion\revisions.py", line 381, in _assert_registered
    if not is_registered(model):
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\reversion\revisions.py", line 319, in is_registered
    return _get_registration_key(model) in _registered_models
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\reversion\revisions.py", line 312, in _get_registration_key
    return (model._meta.app_label, model._meta.model_name)
AttributeError: type object 'ModelBase' has no attribute '_meta'
4

2 回答 2

3

你传递了错误的东西get_for_object——你需要传递实例,function而不是模型,Function

于 2017-03-29T09:24:47.710 回答
1

去掉括号,Django 模板引擎会为你调用:

{% for field_name, field_value in version.field_dict.items %}

这适用于采用零参数的方法。

于 2017-03-29T08:17:03.253 回答