Iterate over model database fields

1,693 views
Skip to first unread message

Matias Surdi

unread,
Jan 8, 2009, 10:22:41 AM1/8/09
to django...@googlegroups.com
Is there any way to iterate over all the fields in a model instance on a
template?

I'd like to have a "default" template which iterates over whatever model
yo give it and prints all its field on a table.


Any help will be appreciated.

bruno desthuilliers

unread,
Jan 8, 2009, 5:34:42 PM1/8/09
to Django users
On 8 jan, 16:22, Matias Surdi <matiassu...@gmail.com> wrote:
> Is there any way to iterate over all the fields in a model instance on a
> template?

Not directly. The fields names are accessible thru the
model_instance._meta attribute, and you cannot access '_protected'
attributes in a template.

> I'd like to have a "default" template which iterates over whatever model
> yo give it and prints all its field on a table.

You'll have to write a (simple) view that returns the model instance's
fields names and values as context. But that's mostly trivial once you
know how to get field names and values from an instance (which has
already been explained here - but feel free to ask if you fail to find
the relevant posts).

Matias Surdi

unread,
Jan 8, 2009, 5:56:16 PM1/8/09
to django...@googlegroups.com
Great!!

I think that the _meta attribute will be enough. I think that if I
define a method on the base model of all my models something like
get_fields() it could then return a list of the fields by accessing
self._meta.fields, and this get_fields should be accessible from
templates,shouldn't it?.

Thanks for your help :-)

bruno desthuilliers

unread,
Jan 9, 2009, 4:36:57 AM1/9/09
to Django users


On 8 jan, 23:56, Matias Surdi <matiassu...@gmail.com> wrote:
> Great!!
>
> I think that the _meta attribute will be enough. I think that if I
> define a method on the base model of all my models something like
> get_fields() it could then return a list of the fields by accessing
> self._meta.fields, and this get_fields should be accessible from
> templates,shouldn't it?.

Yeps, but it has a couple drawbacks IMHO. One of them is that it
wouldn't work on other models. Another one is that it makes all of
your models dependant on a same base class for no good (semantic)
reason. Inheritance is vastly overrated IMHO, and one of the nice
points with Python is that it doesn't force you into "purist" OO when
it doesn't make sense. As far as I'm concerned, I'd make get_fields()
a plain function, so you don't have to inherit from YourModelBase to
make use of it. I don't see any need for polymorphic dispatch here so
better to KISS. And if you found a need for polymorphism, it could
easily be implemented using a custom __magicmethod__, so the generic
get_fields function would either call youmodel.__get_fields__() if
implemented or fall back to the default generic behaviour. But I
really doubt you'll find any use case for such a mechanism !-)

wrt/ using this get_fields function, one simple solution I already
mentionned would be to use a dedicated view. Another one - perhaps
more "reusable" is a simple template filter:

<ul>
{% for field in model|get_fields %}
<li><b>{{ field.name }} :</b> {{ field.value }}</li>
{% endfor %}
</ul>

HTH
Reply all
Reply to author
Forward
0 new messages