在模板中显示 django-reversion 的 field_dict.items()
Posted
技术标签:
【中文标题】在模板中显示 django-reversion 的 field_dict.items()【英文标题】:Show field_dict.items() of django-reversion in template 【发布时间】:2017-08-22 14:42:12 【问题描述】:我想在我的项目中使用django reversion 应用程序。我有点困惑。如何在模板中显示更改列表(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'
【问题讨论】:
【参考方案1】:去掉括号,Django 模板引擎会为你调用:
% for field_name, field_value in version.field_dict.items %
这适用于采用零参数的方法。
【讨论】:
请再看我的帖子。尝试打开页面时出现错误。它说 AttributeError: type object 'ModelBase' has no attribute '_meta' 只有视图函数有问题。你对此有什么想法吗?【参考方案2】:您将错误的东西传递给get_for_object
- 您需要传递实例function
,而不是模型Function
。
【讨论】:
versions = Version.objects.get_for_object(Function.objects.get(pk=id))以上是关于在模板中显示 django-reversion 的 field_dict.items()的主要内容,如果未能解决你的问题,请参考以下文章