Skip to content

Displaying Choice Labels

Our list shows rabbit and available: the values stored in the database. Visitors should see each choice's label from TextChoices, such as "Rabbit" and "Adoption pending".

get_FOO_display()

Every field with choices gets a method on the model, get_FOO_display(), where FOO is the field's name. It returns the label for the field's value.

In a second terminal, start the shell with uv run manage.py shell:

Python REPL
>>> clover = Animal.objects.get(name="Clover")
>>> clover.species
'rabbit'
>>> clover.get_species_display()
'Rabbit'
>>> clover.get_status_display()
'Available'

In a template, it's called without parentheses, like age_label. In animals/templates/animals/animal_list.html, change the line under the name:

animals/templates/animals/animal_list.html
        <p>{{ animal.get_species_display }}, {{ animal.age_label }}, {{ animal.get_status_display }}</p>

Reload http://127.0.0.1:8000/. Clover's line reads Rabbit, Bunny, Available.

Why isn't the label stored in the database?

The column holds only the value. The labels are in the model's code, so changing a label, or translating it, changes every page without touching a row.