Problem:
In models. py of Django, we define some choices tuples, which are similar to some dictionary values. Generally, they are drop-down boxes or single-choice boxes. For example, 0 corresponds to male and 1 corresponds to female.
Class area (models. model): area_level = (0, u 'China'), (1, u 'province, municipality '), (2, u 'city, municipality Region '), (3, U' district, county, etc. '),) areaname = models. charfield (max_length = 30, unique = true, verbose_name = 'region name') code = models. charfield (max_length = 20, blank = true, default = "", verbose_name = 'region Code') parentid = models. integerfield (verbose_name = 'parent id', null = true) level = models. integerfield (choices = area_level, verbose_name = 'hierd', null = true)
There is a table on the page to display the fields in the table. If the database stores 0, it is displayed nationwide, and 1 shows the provinces and municipalities are similar:
Name code level operations in upper-level regions nationwide (0) delete Beijing BJ province and municipality (1) Delete nationwide
Is there any way in Django that we can use directly?
Solution:
We can check the first result by Google:
Http://stackoverflow.com/questions/4320679/django-display-choice-value
Here is the answer. One is to use Django's own method, and the other is to use if to judge (this is obviously not liked by everyone)
The Django documentation is here:
Https://docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.Model.get_FOO_display
On the page, we can directly display the dictionary value as long as we write it like this.
<td>{{ obj.get_level_display }}({{ obj.level }})</td>
OBJ. Get _ field name _ display.
To use the framework well, you still need to read more documents.
This article is from "orangleliu notebook "? Blog, reprint please be sure to keep this source http://blog.csdn.net/orangleliu/article/details/40268093
[Django] choices Dictionary defined in models displays values on the page