[Answered ]-Is There a Way to Query Enumerated Types in Django Based on Values?

2👍

For choice fields for readability Django allows you to specify two values, first is the actual value to be stored in the DB, and the second one will be displayed in forms, etc for readability. For example in

CHOICES = (
    (0, 'No'),
    (1, 'Yes'),
    (2, 'Don\'t Know'),
)

the values which will be stored in the DB would be [0,1,2] however for readability their associated strings will be displayed in forms, etc.

Now as for your problem, the issue here is that you are using an IntegerField for myfield however are trying to filter by a string. Whenever you do filter lookups (filter(...) or exclude(...)), you must provide the values that are related to what is being stored in the database. Therefore whenever you want to filter on an IntegerField you must provide an integer for filtering. I am not sure why Django allowed you to have choices for an IntegerField where the first values are not integers but you should probably change that. If you will use the above example choice values, you can filter for yes or no by

.filter(myfield__in=[0,1])

If you want to filter by the associated string values, there is really no simple method. You have to lookup the stored value related to the string first.

def lookup_choices(choices, values):
    choices = dict(choices)
    rchoices = {choices [k]: k for k in choices}
    return [rchoices[v] for v in values]

.filter(myfield__in=lookup_choices(CHOICES, ['Yes', 'No'])

You however rarely should need to do this because Django will only use associated string values for rendering values (select box) but will always return you the actual values (e.g. form.cleaned_data).

Leave a comment