How do I get the string value of the tuple in Django

Edjeil Dominic B. Labra :

This sounds like a noob question, I just want to ask how do I get the string value of this tuple.

USER_TYPE_CHOICES = (
    (1, 'PQA'),
    (2, 'Tester'),
    (3, 'Test Lead'),
    (4, 'Test manager'),
    (5, 'Senior Test Manager'),
    (6, 'admin'),
)
user_type = models.PositiveSmallIntegerField(choices=USER_TYPE_CHOICES)

When I call the value as:

<h1> {{ user.user_type }} </h1>

It shows up as the int value and not the string.

It sounds very simple but I can't seem to find it on the python documentation.

Jun Zhou :

Using PositiveSmallIntegerField will only show index numbers, you can use models.CharField()

USER_TYPE_CHOICES = (
    ('PQA', 'PQA'),
    ('Tester', 'Tester'),
    ('Lead', 'Test Lead'),
    ('Manager', 'Test manager'),
    ('SeniorManager', 'Senior Test Manager'),
    ('Admin', 'admin'),
)

user_type = models.CharField(max_length=20, choices=USER_TYPE_CHOICES)

You may need to recreate the database and do the migration operations following this link to make the new string choices working.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=27753&siteId=1