Showing choicefield in template Django


For example, we have choicefield in models :

1
2
3
4
5
6
7
8
class Employee(CommonInfo):
    PROFILE_CHOICES = (
       (1, _("Hello")),
       (2, _("World")),
    )

    name = models.CharField(max_length=255, unique=True)
    profile = models.IntegerField(choices=PROFILE_CHOICES , default=1)

To showed value of profile in template, we can use

1
object.get_{name-of-choicefield}_display

So, example for this case will be object.get_profile_display


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.