custom filtering in django rest framework

I am trying to implement custom filtering in my code and went through the documentation. But I couldnt understand the following snippets from the docs.

class UserFilter(django_filters.FilterSet):
    class Meta:
        model = User
        fields = ['username', 'last_login']

# or

class UserFilter(django_filters.FilterSet):
    class Meta:
        model = User
        fields = {
            'username': ['exact', 'contains'],
            'last_login': ['exact', 'year__gt'],
        }

The doc says we can use either one the first or second one but what is the exact difference?? What is the meaning of contains and year_gt?

>Solution :

There’s a link in the docs there for the django lookups docs also, and what you’ll find with this is that you can make searching/filtering more efficient by using that second option.

The first option in the docs, where fields is just a list will do an exact match query against each of the fields you list.

In the second option, hat the filters library will do with the fields dictionary is it’ll use "double underscore" notation to create lookups of;

  • username__exact
  • username__contains
  • last_login__exact
  • last_login__year__gt

You can use the double underscore to access an attribute of something, or the lookup type. So on last_login for that final example above, it’ll take the year of the last_login value and for that year, the lookup type is greater than (gt).

There are lots of these lookup values that can be really helpful to learn. For example, contains is case sensitive but you can use icontains for a case insensitive match. There’s also lt for less than, or lte/gte for less/greater than or equal. Check out the available lookups here.

Leave a Reply