Home > database >  Django-filter. Lookup expression doesn't work properly
Django-filter. Lookup expression doesn't work properly

Time:06-03

When I use MultipleChoiceFilter with lookup_expr='iexact' it looks like the parameters are still case sensitive and Select a valid choice is returned. What am I doing wrong?

It's my filter class:

class PostFilter(filters.FilterSet):
    lang = filters.MultipleChoiceFilter(
        field_name='language', choices=[('ENG', 'ENG'), ('DEU', 'DEU')], lookup_expr='iexact')

    class Meta:
        fields = ('lang',)
        model = Post

Request: posts/?lang=eng&lang=deu

Response: Select a valid choice. eng is not one of the available choices.

My DB: PostgreSQL 14

CodePudding user response:

Your request should be posts/?lang=ENG&lang=DEU as the values will first be validated against the definition in the filter class. lookup_expr='iexact' will be used later for the database lookup. If you want to use the lower case values you would have to change your choices to choices=[('eng', 'ENG'), ('deu', 'DEU')].

  • Related