LoginSignup
3
2

More than 3 years have passed since last update.

Djangoのモデルでchoicesを使用した値をtemplateで表示したい場合

Posted at

問題点

choicesを使用した際に、template側でchoicesのvalueを表示したいことがあった。
そこで、 get_xxx_display を使用することで表示出来たので、メモしておきたいと思います。

モデル

AREA_NAME_ASIA = 'asia'
AREA_NAME_EUROPE = 'europe'

AREA_NAME_CHOICES = (
    (AREA_NAME_ASIA, 'アジア'),
    (AREA_NAME_CHOICES, 'ヨーロッパ'),
)

area = models.CharField(max_length=50, choices=AREA_NAME_CHOICES, verbose_name='エリア名')

template側

{{ obj.get_area_display }}

これで、 アジア と表示することが出来る。
ドキュメントはちゃんと読まないといけないですね、、

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2