I've opened a ticket (#21370) with a proposal for the *inclusion of a
generic "getter" filter* in the built-in library.
*Why?*
Well, because at the moment Django's template system really lacks of
support for retrieving data from existing context variables in all the
cases in which the name of the attribute we need is determined at execution
time.
*Syntax:*
{{ object|get:attr_name }}
Of course the idea is to add a multi-purpose filter which covers different
use cases (model instances, arbitrary objects, dicts, lists, tuples, etc.)
*Some examples of its use:*
*
*
*Access to model data based on a dynamic list of fields*
<table>
{% for obj in object_list %}
<tr>
{% for f in field_list %}
<td>{{ obj|get:f }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
*Access to dict values based on a dynamic list of keys*
{% for key in key_list %}
{{ dict|get:key }}
{% endfor %}
*Access to a variable index of a list*
{{ list|get:var_index }}
*Ticket link:*
https://code.djangoproject.com/ticket/21370
--
You received this message because you are subscribed to the Google Groups
"Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-developers/619709ac-6536-4222-8052-a0a6ba9ccdcc%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.