[Django]-Get key value from display value from choices in Django

8👍

Build a dict of the display_value: value pairs and get the value from it:

CATEGORIES_D = {v: k for k, v in CATEGORY_CHOICES}
category = CATEGORIES_D[category]

0👍

How about this:

choices_dict = {}
for choice, value in CATEGORY_CHOICES:
    choices_dict[value] = choice

category = choices_dict[category]

Leave a comment