Opened 14 years ago

Last modified 12 years ago

#16034 closed New feature

get_field_display: Django needs a filter to allow get_field_display for a dynamic field — at Version 4

Reported by: thepapermen Owned by: nobody
Component: Template system Version: 1.3
Severity: Normal Keywords:
Cc: thepapermen, Miguel Araujo Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Aymeric Augustin)

{{ SomeModel.get_field_display }} is nice, but it is handy only for a specified field.

If One wants to write a more universal template which should work with any set of fields, this won't work.

What I propose is a universal filter which should provide human-readable name for a field of any type.

For example, like this:

<table>

{% for item in query %}

<tr>

{% for field in fields %}

<td>{{item|human_readable:field}}</td>

{% endfor %}

</tr>

{% endfor %}

<table>

This will be a simple yet powerful way to provide nice human-readable info from Django models and forms.

Change History (4)

comment:1 by thepapermen, 14 years ago

I'm talking about something like this:

def human_readable(value, arg):

    if hasattr(value, 'get_%s_display' % arg):
        return getattr(value, 'get_%s_display' % arg)()
    elif hasattr(value, str(arg)):
        return getattr(value, arg)
    else:
        return settings.TEMPLATE_STRING_IF_INVALID

register.filter('human_readable', human_readable)

But secure against showing _meta attributes.

Last edited 14 years ago by thepapermen (previous) (diff)

comment:2 by thepapermen, 14 years ago

Cc: thepapermen added

comment:3 by Miguel Araujo, 14 years ago

Cc: Miguel Araujo added

comment:4 by Aymeric Augustin, 13 years ago

Description: modified (diff)
Triage Stage: UnreviewedDesign decision needed

The idea and the API should be discussed on the mailing list before this ticket can be accepted.

Note: See TracTickets for help on using tickets.
Back to Top