How to get the "name" of a field whose value is limited by the choice attribute in Django's model

If there was such a model,

models.py


from django.db import models


class Example(models.Model):
    EXAMPLE_FOO = ((1, 'foo1'), (2, 'foo2'))
    foo = models.IntegerField(choices=EXAMPLE_FOO)

If you want to get the "name" of the field foo, that is, "foo1" or "foo2", the get_foo_display method is defined without you having to write your own code, so use this.

Don't waste your time creating foo_name properties! (Example: I)

【reference】 http://stackoverflow.com/questions/4320679/django-display-choice-value https://docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.Model.get_FOO_display

Recommended Posts

How to get the "name" of a field whose value is limited by the choice attribute in Django's model
How to get the last (last) value in a list in Python
How to get the vertex coordinates of a feature in ArcPy
How to get a list of files in the same directory with python
How to get the variable name itself in python
How to get the number of digits in Python
How to check in Python if one of the elements of a list is in another list
How to set the html class attribute in Django's forms.py
How to get a list of built-in exceptions in python
How to get a quadratic array of squares in a spiral!
Get the value of a specific key up to the specified index in the dictionary list in Python
How to create a record by pasting a relation to the inheriting source Model in the Model inherited by Django
How to check if the contents of the dictionary are the same in Python by hash value
I tried to create a Python script to get the value of a cell in Microsoft Excel
When a character string of a certain series is in the Key of the dictionary, the character string is converted to the Value of the dictionary.
How to get and set the NTP server name by DHCP
How to determine the existence of a selenium element in Python
How to get all the possible values in a regular expression
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
How to find the memory address of a Pandas dataframe value
How to get a list excluding elements whose index is i ...?
Create a function to get the contents of the database in Go
How to get a specific column name and index name in pandas DataFrame
[Linux] Command to get a list of commands executed in the past
How to get a value from a parameter store in lambda (using python)
How to get a stacktrace in python
[NNabla] How to add a quantization layer to the middle layer of a trained model
How to get a namespaced view name from a URL (path_info) in Django
I tried to display the altitude value of DTM in a graph
How to sort by specifying a column in the Python Numpy array.
Python scikit-learn A collection of predictive model tips often used in the field
How to get the notebook name you are currently using in Google Colab
To output a value even in the middle of a cell with Jupyter Notebook
[NNabla] How to get the output (variable) of the middle layer of a pre-built network
How to count the number of elements in Django and output to a template
[python] How to sort by the Nth Mth element of a multidimensional array
A memorandum of how to execute the! Sudo magic command in Jupyter Notebook
How to change the appearance of unselected Foreign Key fields in Django's ModelForm
How to use loc / iloc / ix to get by specifying a column in CASTable
Python scikit-learn A collection of predictive model tips often used in the field
[Introduction to Python] How to get the index of data with a for statement
How to calculate the volatility of a brand
Get the caller of a function in Python
How to get the files in the [Python] folder
Search by the value of the instance in the list
How to display the modification date of a file in C language up to nanoseconds
How to achieve access by attribute and retention of insertion order in Python dict
How to identify the element with the smallest number of characters in a Python list?
[Ruby] How to replace only a part of the string matched by the regular expression?
How to specify a schema in Django's database settings
How to retrieve the nth largest value in Python
Get the variable name of the variable as a character string.
Get the file name in a folder using glob
How to get a logged-in user with Django's forms.py
How to visualize the decision tree model of scikit-learn
[Python] Get element by specifying name attribute in BeautifulSoup
How to use the exists clause in Django's queryset
How to use the model learned in Lobe in Python
How to unprefix the DB name used by pytest-django
How to find out if there is an arbitrary value in "somewhere" of pandas DataFrame