Public method for getting model field names in order of definition

1,893 views
Skip to first unread message

David Chandek-Stark

unread,
Oct 21, 2009, 10:38:31 AM10/21/09
to Django developers
The values_list() query set method is useful for dumping data to CSV,
etc. However, I find that I often want to use it without specifying
the field names (to get them all) and yet also include the field names
as the first row in my data export. There is no "public" method for
getting all the names of a model's fields in the order of definition.
Model._meta.get_all_field_names() returns a sorted list, and one has
to read the source code to discover that one should (probably) use the
"attname" attribute of a field for its name rather than the "name"
attribute. The ValuesQuerySet superclass of ValuesListQuerySet by
default sets its field names with:

[f.attname for f in self.model._meta.fields]

So, my question is: Do folks think that it would be good to have a
public method for getting the field names in order of definition?

Thanks,
David

Rob Hudson

unread,
Nov 20, 2009, 11:25:31 PM11/20/09
to django-d...@googlegroups.com
I do, and have another use case in mind...

For example, let's say you have a Profile model and you have a profile
edit view which uses a ModelForm. In the edit template you can simply
output the {{ form }} or iterate over the form:

{% for field in form %}
# output field
{% endfor %}

But if you wanted to redisplay that information back to the user in a
read-only form, there is no iterative way to do so (as far as I know).
Instead you must specifically "hard code" each field:

<dl>
<dt>Field 1</dt>
<dd>{{ profile.field1 }}</dd>
....
</dl>

This doesn't solve your use case, but I'd like to see some sort of
ModelDisplay, which mimics ModelForm, and can be used in such a way to
either specify select fields or exclude fields:

class ProfileDisplay(ModelDisplay):
class Meta:
model = Profile
exclude = ('user',)

You could then pass this to your template and iterate over it:

<dl>
{% for field in profile_display %}
<dt>{{ field.field_name }}</dt>
<dd>{{ field.value }}</dd>
{% endfor %}
</dl>

In the past I've discovered the model_to_dict in
django/forms/models.py that could be used for this if only it used a
SortedDict instead of a plain dict. With this minor change...

@@ -121,7 +121,7 @@ def model_to_dict(instance, fields=None, exclude=None):
# avoid a circular import
from django.db.models.fields.related import ManyToManyField, OneToOneField
opts = instance._meta
- data = {}
+ data = SortedDict()
for f in opts.fields + opts.many_to_many:
if not f.editable:
continue

You could then, in your view, do:

profile_display = model_to_dict(profile_obj, exclude=('id', 'user'))

And in the template, do:

<dl>
{% for field_name, value in profile_display.items %}
<dt>{{ field_name }}</dt>
<dd>{{ value }}</dd>
{% endfor %}
</dl>

Which I think would be a nice shortcut for template creators and also
reduces code change as you adapt your models.

All that said, I'm not sure if I'd propose this change for this
purpose... or if we did the method might better live somewhere else.
The model_to_dict method is used specifically for Forms as the
docstring cites: Returns a dict containing the data in ``instance``
suitable for passing as a Form's ``initial`` keyword argument.

Is this a use case others could use?

-Rob

Anssi Kaariainen

unread,
Nov 21, 2009, 8:57:39 AM11/21/09
to Django developers


On Nov 21, 6:25 am, Rob Hudson <r...@cogit8.org> wrote:
> But if you wanted to redisplay that information back to the user in a
> read-only form, there is no iterative way to do so (as far as I know).
>  Instead you must specifically "hard code" each field:
>
>     <dl>
>         <dt>Field 1</dt>
>         <dd>{{ profile.field1 }}</dd>
>         ....
>     </dl>

If I am not completely mistaken you can use your ModelForm with
field.label and field.data to get read-only view of the model.

Anssi Kääriäinen

Richard Laager

unread,
Nov 21, 2009, 4:41:57 PM11/21/09
to django-d...@googlegroups.com
On Wed, Oct 21, 2009 at 6:38 AM, David Chandek-Stark <dchand...@gmail.com> wrote:
> one should (probably) use the
> "attname" attribute of a field for its name rather than the "name"
> attribute.

Why's that? I'm using the "verbose_name" attribute of the field and
capitalizing the first letter to match the admin interface's behavior.

Richard

Richard Laager

unread,
Nov 22, 2009, 7:14:47 PM11/22/09
to django-d...@googlegroups.com
On Sat, 2009-11-21 at 05:57 -0800, Anssi Kaariainen wrote:
> If I am not completely mistaken you can use your ModelForm with
> field.label and field.data to get read-only view of the model.

I was trying to do something like this today and didn't have any luck.
Do you have a pointer to a working sample?

Richard

signature.asc

Anssi Kaariainen

unread,
Nov 23, 2009, 7:56:45 AM11/23/09
to Django developers
Here is a working example. I am using svn version of Django, but IIRC
this works also in 1.0.

views.py:

from django.shortcuts import render_to_response
from django import forms

class Example(forms.Form):
field1 = forms.CharField()
field2 = forms.IntegerField(label='Labeled')

def view(request):
data = {'field1': 'Foo', 'field2': 10}
form = Example(data=data)
return render_to_response('t.html', {'form': form})

t.html:

{% for field in form %}
{{ field.label }}{{field.data}}
{% endfor %}
>  signature.asc
> < 1KViewDownload

Richard Laager

unread,
Nov 23, 2009, 12:03:38 PM11/23/09
to django-d...@googlegroups.com
On Mon, 2009-11-23 at 04:56 -0800, Anssi Kaariainen wrote:
> Here is a working example.
> data = {'field1': 'Foo', 'field2': 10}
> form = Example(data=data)

Thanks for the example, but that's not a ModelForm.

Richard

Anssi Kaariainen

unread,
Nov 23, 2009, 1:37:34 PM11/23/09
to Django developers
> Thanks for the example, but that's not a ModelForm.
>
> Richard

Another try, view.py:

from django.forms.models import model_to_dict

def view(request):
data = Foo.objects.all()[0]
print data.start
form = Example(data=model_to_dict(data))
return render_to_response('t.html', {'form': form})

Where Example is a ModelForm for model Foo.

Seems like there is no easy way to get access to the initial data of a
field in a template. Example(instance=foo) will only populate the
forms initial dict but not data. Should there be a similar property
for initial in BaseForm as there is for data?

Anssi Kääriäinen
Reply all
Reply to author
Forward
0 new messages