Django templates: verbose version of a choice

前端 未结 8 1138
野性不改
野性不改 2020-11-30 18:16

I have a model:

from django.db import models

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

class MealOrder(models.Model):
          


        
8条回答
  •  执笔经年
    2020-11-30 18:28

    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 %}
        
            {{ field.label }}:
            {{ field.data|display }}
        
    {% endfor %}
    

提交回复
热议问题