[Django]-Django: Values_list returns id from choice field instead of name

-1đź‘Ť

if you choose to use the values_list() function, then you’ll have to do the lookup/conversion yourself. (That’s because Django is storing the number in the database, and not the string, and values_list() says “get me what is in the database”.) Doing the lookup is not hard — you can create a dictonary as

my_choices_dict = dict(my_choices)

and use that.

Alternatively, if you get the full object, you can use the get_field1_display() method that is built into your model class. See https://docs.djangoproject.com/en/1.8/topics/db/models/ and search for “get_FOO_display”

👤Chris Curvey

Leave a comment