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
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.
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.
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.
There's a method for that! (™ Apple)
fb.get_user_choices_display()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With