7

Possible Duplicate:
Django - Iterate over model instance field names and values in template

Hi,

I'm trying to list fields and corresponding values of generic Django models in the templates. However I can't find an inbuilt solution for a fairly common problem. I'm pretty close to the solution but can't find a way out.

view.py Code:

def showdetails(request, template):
    objects = newivr1_model.objects.all()
    fields = newivr1_model._meta.get_all_field_names()
    return render_to_response(template, {'fields': fields,'objects':objects},
        context_instance=RequestContext(request))

template code:

    <table>                                                                                                                                       
    {% for object in objects %}                                                 
        <tr>                                                                    
            {% for field in fields %}                                           
                <td>                                                            
            <!--    {{ object.field }} /*This line doesn't work*/ -->                                           
                </td>                                                           
            {% endfor %}                                                        
        </tr>                                                                   
    {% endfor %}                                                                
    </table>

What should I be doing at the commented template line so that i get the value of Object.field?

Any better DRY methods are most welcome as well.

Community
  • 1
  • 1
Neo
  • 13,179
  • 18
  • 55
  • 80

2 Answers2

5

Unfortunately, you can't do lookups like that in the template engine.

You'll have to deal with that in the view.

def showdetails(request, template):
    objects = newivr1_model.objects.all()

    for object in objects:
        object.fields = dict((field.name, field.value_to_string(object))
                                            for field in object._meta.fields)

   return render_to_response(template, { 'objects':objects },
                                    context_instance=RequestContext(request))

Template

{% for object in objects %}
    <tr>
    {% for field, value in object.fields.iteritems %}
        <td>{{ field }} : {{ value }}</td>
    {% endfor %}
    </tr>
{% endfor %}
Yuji 'Tomita' Tomita
  • 115,817
  • 29
  • 282
  • 245
3

You need to create your own filter, that will work like getattr in python and use it in the template:

{{ object|getattribute:field }}

Here there is description how to do that: Performing a getattr() style lookup in a django template

But I don't think that's really a good idea. Insted try to do this logic in the view, like this:

object_values = []
for object in objects
   object_values.append([])
   for field in fields:
       object_values[-1].append(getattr(object, field))
return render_to_response(template, {'object_values': object_values},
        context_instance=RequestContext(request))

and in the template:

<table>                                                                                                                                       
{% for values in object_values %}                                                 
    <tr>                                                                    
        {% for value in values %}                                           
            <td>                                                            
                {{ value }}
            </td>                                                           
        {% endfor %}                                                        
    </tr>                                                                   
{% endfor %}                                                                
</table>

Django template system doesn't provide many function (filters), because you are supposed to do all the logic in the views. Template should only present data.

Community
  • 1
  • 1
gruszczy
  • 40,948
  • 31
  • 128
  • 181
  • gruszczy - I'm only trying to present data in templates :) . And in theory this should be a fairly simple and common problem too. – Neo Mar 18 '11 at 23:38
  • Then using getattr filter should help you. I believe it is a common problem. – gruszczy Mar 18 '11 at 23:41