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