Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django print choices value

EMP_CHOICES = (          (0,'-- Select --'),          (1,'Good'),          (2,'Average'),      )  class EMPFeedback(models.Model):      user_choices = models.IntegerField(choices=EMP_CHOICES) 

If the value stored in the db as 1 for user_choices how to print the corresponding user_choices corresponding value (i.e. 1==GOOD)

fb = EMPFeedback.objects.get(id=1) print fb.user_choices  # prints 1 print fb.user_choices.EMP_CHOICES  
like image 263
Rajeev Avatar asked Nov 25 '10 06:11

Rajeev


People also ask

How to get choice value in Django?

To display choice value with Python Django, we call the get_FOO_display() method in our template. to create the Person model with the gender field. to return the results of the get_gender_display method where person is a Person instance.

How do I display in Django?

Add template folder in the django folder and provide its path in django_folder > settings.py . Create a file named as urls.py in the app folder and provide its path in django_project > urls.py. Make a function for the index html page in app_folder > views.py. Add html code for the in django_project > template > index.

What is choices in Django model?

Choices limits the input from the user to the particular values specified in models.py . If choices are given, they're enforced by model validation and the default form widget will be a select box with these choices instead of the standard text field.


1 Answers

There's a method for that! (™ Apple)

fb.get_user_choices_display() 
like image 185
Ignacio Vazquez-Abrams Avatar answered Sep 22 '22 09:09

Ignacio Vazquez-Abrams