[Answered ]-How to access a specific element from a CHOICES list in Django?

1👍

You could write a function like below adn get it

 def get_choice(all_choices,search):
      for choice in all_choices:
        if choice[0] == search:
          return choice[1]
 return None    
 get_choice(all_choices,'JR')

0👍

Possible duplicate

Use

get_<field_name>_display()

0👍

try this i think using using dictionary will be more efficient than list.the time complexity for dictionary is O(1) and for list it is O(n).

search = 'JR'
all_choices = Graduation.YEAR_IN_SCHOOL_CHOICES
dictionnary = {i[0]:i[1] for i in all_choices}
print(dictionnary[search])

Leave a comment