[PYTHON] If you want to display values using choices in a template in a Django model

problem

When using choices, I sometimes wanted to display the value of choices on the template side. Therefore, I was able to display it by using get_xxx_display, so I would like to make a note of it.

model

AREA_NAME_ASIA = 'asia'
AREA_NAME_EUROPE = 'europe'

AREA_NAME_CHOICES = (
    (AREA_NAME_ASIA, 'Asia'),
    (AREA_NAME_CHOICES, 'Europe'),
)

area = models.CharField(max_length=50, choices=AREA_NAME_CHOICES, verbose_name='Area name')

template side

{{ obj.get_area_display }}

Now you can display Asia. You have to read the document properly, right?

Recommended Posts

If you want to display values using choices in a template in a Django model
If you want to assign csv export to a variable in python
[Django] What to do if the model you want to create has a large number of fields
If you want to create a Word Cloud.
A note that you want to manually decorate the parameters passed in the Django template form item by item
I want to use complicated four arithmetic operations in the IF statement of the Django template! → Use a custom template
If you want to make a TODO application (distributed) now using only Python
If you specify a foreign key in Django's model change, you need to specify on_delete.
When you want to plt.save in a for statement
[Django] A memorandum when you want to communicate asynchronously [Python3]
How to generate a query using the IN operator in Django
If you want to become a data scientist, start with Kaggle
What to do if you get a minus zero in Python
Check if you can connect to a TCP port in Python
When you want to replace multiple characters in a string without using regular expressions in python3 series
[Django 2.2] Add a New badge to new posts with a date using a template filter
I want to print in a comprehension
How to make a model for object detection using YOLO in 3 hours
If you get a Programming Error: (1146, "Table'<table name>' doesn't exist") in Django
If you want to switch the execution user in the middle of a Fabric task, settings context manager
[Python] If you want to draw a scatter plot of multiple clusters
If you want to get multiple statistics with groupby in pandas v1
Get a reference model using Django Serializer
If you want to count words in Python, it's convenient to use Counter.
[Python] What to do if you get a ModuleNotFoundError when importing pandas using Jupyter Notebook in Anaconda
If you were to create a TODO application (distributed) using only Python-extension 1
[Python] [Word] [python-docx] Try to create a template of a word sentence in Python using python-docx
Implement a Custom User Model in Django
I want to create an API that returns a model with a recursive relationship in the Django REST Framework
If you want to make a Windows application (exe) that can be actually used now using only Python
What to do if you get a "No versions found" error in pipenv
AssertNumQueries is useful if you want to easily test N + 1 queries with django
How to count the number of elements in Django and output to a template
Precautions when adding items using DateField to an existing model later in Django
What to do if you get "Python not configured." Using PyDev in Eclipse
[Python] What to check when you get a Unicode Decode Error in Django
If you want to make a discord bot with python, let's use a framework
I want to easily create a Noise Model
I want to pin Datetime.now in Django tests
I want to create a window in Python
How to create a Rest Api in Django
Until you create a new app in Django
I want to display the progress in Python!
I want to upload a Django app to heroku
How to use fixture in Django to populate sample data associated with a user model
I want to set up a mock server for python-flask in seconds using swagger-codegen.
Understand Python yield If you put yield in a function, it will change to a generator
What to do if you don't want to use Japanese column names when using ortoolpy.logistics_network
Create a shogi game record management application using Django 5 ~ Pass DB data to Template ~
I want to easily implement a timeout in python
What to do if you can't create a virtual environment using venv even though you're following the Django official website
I want to transition with a button in flask
(Python) Try to develop a web application using Django
I want to write in Python! (2) Let's write a test
If you want to use Cython, also include python-dev
I want to randomly sample a file in Python
TemplateView patterns you'll want to learn first in Django
When you want to filter with Django REST framework
I want to work with a robot in python.
When you want to play a game via Proxy
How to display DataFrame as a table in Markdown