Django Display Choice Value
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Django model choices solve a common problem: storing compact values in the database while showing friendly labels to users. The pattern is simple, but many projects still display raw codes in templates, APIs, or admin views. This article shows how to display choice labels cleanly across models, forms, templates, and serializers.
Define Choices Clearly in the Model
A choice field stores the machine value and maps it to a human-readable label. Modern Django code should prefer TextChoices for readability and safer references.
This gives you a compact value in the database and a stable place to reference valid states in code.
Use get_FIELD_display for Human Labels
Django automatically adds a method named get_<field>_display for fields that define choices. This is the most direct way to show labels.
In templates, call the method without parentheses.
That small change prevents UI bugs where users see one-letter state codes.
Use Choice Labels in Forms and Admin
Django forms and admin usually render labels automatically when the field has choices. You can still customize behavior to keep labels consistent.
When this form renders status, users see Pending, Paid, and Canceled, not P, D, or C. In admin list views, add a helper to show labels explicitly.
API Serialization Patterns
If you expose data through an API, decide whether clients should receive values, labels, or both. Returning both is often safest.
This keeps backend logic compact while giving frontend code exactly what it needs.
Internationalization and Consistent Labels
If your app supports multiple languages, keep labels translatable at the model layer. Django can localize choice labels cleanly when you wrap them with lazy translation. This ensures templates, forms, and admin all use the same translated text.
Remember that filtering still uses stored values, not translated labels.
This split is intentional: database values remain stable while labels adapt to user locale.
Common Pitfalls
The most common mistake is hardcoding labels in templates with manual if statements. That duplicates logic and drifts from model definitions. Another issue is storing label text directly in the database instead of stable short codes, which makes localization and migration harder. Teams also sometimes mix old tuple based choices and enum based choices in the same model area, making the code harder to navigate. Finally, API payloads often ship only the compact value, then frontend teams reimplement a mapping table. Return a label field when client readability matters.
Summary
- Use
TextChoicesto define choice values and labels in one place. - Display labels with
get_FIELD_displayin Python code and templates. - Let forms and admin render labels from model choices.
- For APIs, consider returning both stored value and display label.
- Avoid duplicated mapping logic across templates and frontend clients.

