Nurzhan Nogerbek
Nurzhan Nogerbek

Reputation: 5246

Show field_dict.items() of django-reversion in template

I am tring to use django reversion app in my project. I am little bit comfused. How to show list of changes (field_dict.items()) in template?

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 %}

ERROR:

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'

Upvotes: 3

Views: 488

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599778

You're passing the wrong thing into get_for_object - you need to pass the instance, function, not the model, Function.

Upvotes: 3

Moses Koledoye
Moses Koledoye

Reputation: 78554

Drop the parens and the Django template engine will do the calling for you:

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

This works for methods that take zero arguments.

Upvotes: 1

Related Questions