我想在我的项目中使用django reversion app。我有点被困。如何在模板中显示更改列表(field_dict.items())?
models.py :
@reversion.register()
class Function(models.Model):
***FIELDS***
views.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'
答案 0 :(得分:2)
您将错误的内容传递给get_for_object
- 您需要传递实例function
,而不是模型,Function
。
答案 1 :(得分:1)
删除parens,Django模板引擎将为您进行呼叫:
{% for field_name, field_value in version.field_dict.items %}
这适用于采用零参数的方法。