Code

Opened 3 years ago

Closed 16 months ago

#16034 closed New feature (wontfix)

get_field_display: Django needs a filter to allow get_field_display for a dynamic field

Reported by: thepapermen Owned by: nobody
Component: Template system Version: 1.3
Severity: Normal Keywords:
Cc: thepapermen, maraujop 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 aaugustin)

{{ 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.

Attachments (0)

Change History (6)

comment:1 Changed 3 years ago by thepapermen

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

I'm talking about something like this:

def human_readable(value, arg):

    if hasattr(value, 'get_' + str(arg) + '_display'):
        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.

Version 0, edited 3 years ago by thepapermen (next)

comment:2 Changed 3 years ago by thepapermen

  • Cc thepapermen added

comment:3 Changed 3 years ago by maraujop

  • Cc maraujop added

comment:4 Changed 3 years ago by aaugustin

  • Description modified (diff)
  • Triage Stage changed from Unreviewed to Design decision needed

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

comment:5 Changed 3 years ago by thepapermen

  • UI/UX unset

Updating the hypothetical solution:

def human_readable(value, arg):
  if hasattr(value, 'get_' + str(arg) + '_display'):
      return getattr(value, 'get_%s_display' % arg)()
  elif hasattr(value, str(arg)):
      if callable(getattr(value, str(arg))):
          return getattr(value, arg)()
      else:
          return getattr(value, arg)
  else:
      try:
          return value[arg]
      except KeyError:
          return settings.TEMPLATE_STRING_IF_INVALID

comment:6 Changed 16 months ago by aaugustin

  • Resolution set to wontfix
  • Status changed from new to closed

This doesn't look sufficiently general-purpose for inclusion in Django.

Django's contributing guide recommends to discuss feature requests on the django-developers mailing list. Please make you case there and re-open the ticket if the thread shows sufficient interest in this feature.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.