In my models, I have a choices list, such as:
class Graduation(models.Model):
YEAR_IN_SCHOOL_CHOICES = [
('FR', 'Freshman'),
('SO', 'Sophomore'),
('JR', 'Junior'),
('SR', 'Senior'),
('GR', 'Graduate'),
]
student_level = models.CharField(max_length=2, choices=YEAR_IN_SCHOOL_CHOICES)
In my views, I want to get the value for a specific element. I have this:
search = 'JR'
all_choices = Graduation.YEAR_IN_SCHOOL_CHOICES
My goal is to get Junior
returned by the system. How can I do that?
I tried:
all_choices[search]
but that doesn't work. I know I can change the format of my choices list in the models, but in this case my models can not be changed. What is the right way to obtain the label from this list? Do I have to loop through the list and use an if
statement? That doesn't seem efficient but I am not sure if I have another choice.