On Wed, Oct 21, 2009 at 6:38 AM, David Chandek-Stark
<[email protected]> wrote:
>
> 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?
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
--
You received this message because you are subscribed to the Google Groups
"Django developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-developers?hl=.