ChatGPT解决这个技术问题 Extra ChatGPT

Django templates: verbose version of a choice

I have a model:

from django.db import models

CHOICES = (
    ('s', 'Glorious spam'),
    ('e', 'Fabulous eggs'),
)

class MealOrder(models.Model):
    meal = models.CharField(max_length=8, choices=CHOICES)

I have a form:

from django.forms import ModelForm

class MealOrderForm(ModelForm):
    class Meta:
        model = MealOrder

And I want to use formtools.preview. The default template prints the short version of the choice ('e' instead of 'Fabulous eggs'), becuase it uses

{% for field in form %}
<tr>
<th>{{ field.label }}:</th>
<td>{{ field.data }}</td>
</tr>
{% endfor %}.

I'd like a template as general as the mentioned, but printing 'Fabulous eggs' instead.

[as I had doubts where's the real question, I bolded it for all of us :)]

I know how to get the verbose version of a choice in a way that is itself ugly:

{{ form.meal.field.choices.1.1 }}

The real pain is I need to get the selected choice, and the only way coming to my mind is iterating through choices and checking {% ifequals currentChoice.0 choiceField.data %}, which is even uglier.

Can it be done easily? Or it needs some template-tag programming? Shouldn't that be available in django already?


r
rob

In Django templates you can use the "get_FOO_display()" method, that will return the readable alias for the field, where 'FOO' is the name of the field.

Note: in case the standard FormPreview templates are not using it, then you can always provide your own templates for that form, which will contain something like {{ form.get_meal_display }}.


yes, I know. It's not as general (universal), though - unless you know a way to iterate in a template over all get_FOO_display methods of a model object :) I'm a bit too lazy for writing non-generic templates ;) Moreover, the docs say it's a model instance's method. Therefore it'd have to be a model form bound to an existing object which is not the case and also not general.
Note that this usage is not limited to the views, get_FOO_display() is a method on the model object itself so you can use it in model code, too! For example, in __unicode__() it is very handy
R
Reema

The best solution for your problem is to use helper functions. If the choices are stored in the variable CHOICES and the model field storing the selected choice is 'choices' then you can directly use

 {{ x.get_choices_display }}

in your template. Here, x is the model instance. Hope it helps.


Why would you answer like this 2 years after a useful answer is already in place? And who would vote it up? Its the same answer as @roberto just 2 years later....
@Mark0978 the reason for upvoting this answer is because (for me) it was clearer to follow then the "top voted" answer. YMMV.
C
Corey Adler

My apologies if this answer is redundant with any listed above, but it appears this one hasn't been offered yet, and it seems fairly clean. Here's how I've solved this:

from django.db import models

class Scoop(models.Model):
    FLAVOR_CHOICES = [
        ('c', 'Chocolate'),
        ('v', 'Vanilla'),
    ]

    flavor = models.CharField(choices=FLAVOR_CHOICES)

    def flavor_verbose(self):
        return dict(Scoop.FLAVOR_CHOCIES)[self.flavor]

My view passes a Scoop to the template (note: not Scoop.values()), and the template contains:

{{ scoop.flavor_verbose }}

A
Artur Gajowy

Basing on Noah's reply, here's a version immune to fields without choices:

#annoyances/templatetags/data_verbose.py
from django import template

register = template.Library()

@register.filter
def data_verbose(boundField):
    """
    Returns field's data or it's verbose version 
    for a field with choices defined.

    Usage::

        {% load data_verbose %}
        {{form.some_field|data_verbose}}
    """
    data = boundField.data
    field = boundField.field
    return hasattr(field, 'choices') and dict(field.choices).get(data,'') or data

I'm not sure wether it's ok to use a filter for such purpose. If anybody has a better solution, I'll be glad to see it :) Thank you Noah!


+1 for mentioning your path #annoyances/templatetags/... LOL ... I use get_FOO_display(), which is mentioned on the bottom of the form docs.
great idea with the use of hasattr on choices!
C
Community

We can extend the filter solution by Noah to be more universal in dealing with data and field types:

<table>
{% for item in query %}
    <tr>
        {% for field in fields %}
            <td>{{item|human_readable:field}}</td>
        {% endfor %}
    </tr>
{% endfor %}
</table>

Here's the code:

#app_name/templatetags/custom_tags.py
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
register.filter('human_readable', human_readable)

Seems quite universal :) Can't tell for sure, because I haven't done too much Python or Django since that time. It's pretty sad, though, that it still needs a 3rd party (not included in Django) filter (otherwise you'd tell us, Ivan, wouldn't you? ;))...
@ArturGajowy Yes, as of today there is no such default feature in Django. I have proposed it, who knows, maybe it will be approved.
PERFECT! WORKS LIKE A CHARM! CUSTOM TEMPLATE FILTERS ROX! THANK YOU! :-)
N
Noah Medling

I don't think there's any built-in way to do that. A filter might do the trick, though:

@register.filter(name='display')
def display_value(bf):
    """Returns the display value of a BoundField"""
    return dict(bf.field.choices).get(bf.data, '')

Then you can do:

{% for field in form %}
    <tr>
        <th>{{ field.label }}:</th>
        <td>{{ field.data|display }}</td>
    </tr>
{% endfor %}

M
Mohamed OULD EL KORY

You have Model.get_FOO_display() where FOO is the name of the field that has choices.

In your template do this :

{{ scoop.get_flavor_display }}

I
Igor Pomaranskiy

Add to your models.py one simple function:

def get_display(key, list):
    d = dict(list)
    if key in d:
        return d[key]
    return None

Now, you can get verbose value of choice fields like that:

class MealOrder(models.Model):
    meal = models.CharField(max_length=8, choices=CHOICES)

    def meal_verbose(self):
        return get_display(self.meal, CHOICES)    

Upd.: I'm not sure, is that solution “pythonic” and “django-way” enough or not, but it works. :)


a
alfonsrv

The extended-extended version of Noah's and Ivan's solution. Also fixed Noah's solution for Django 3.1, as ModelChoiceIteratorValue is now unhashable.

@register.filter
def display_value(value: Any, arg: str = None) -> str:
    """Returns the display value of a BoundField or other form fields"""
    if not arg:  # attempt to auto-parse
        # Returning regular field's value
        if not hasattr(value.field, 'choices'): return value.value()
        # Display select value for BoundField / Multiselect field
        # This is used to get_..._display() for a read-only form-field
        # which is not rendered as Input, but instead as text
        return list(value.field.choices)[value.value()][1]

    # usage: {{ field|display_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)()
        return getattr(value, arg)

    return value.get(arg) or ''