Iterating through model attributes using assignment_tags in django 1.4 templates

Documentation: Django Assignment Tags

/project/myapp/templatetags/app_admin_tags.py:

from django import template
from myapp.models import *



@register.assignment_tag(name="object_attrs")
def object_attrs(obj):
        obj_dict ={}
        for attr in obj._meta.get_all_field_names():
           obj_dict[attr] = obj.__getattribute__(attr)
        return obj_dict

Template:

{% load app_admin_tags %}
{% for obj in objects %}
        {% object_attrs obj as obj_dict %}
        {% for key,value in obj_dict.items %}
           {{key}}: {{ value }}
        {% endfor %}
{% endfor %}
Tagged as assignment_tag , custom django
Written by Andrew Konkol on July 20th, 2012

0 Comments

Log in with Twitter, Google, Facebook, LinkedIn to leave a comment.