Queryset filters

Django build query attributes based on model fields

Consider Tags and Recipe model

1) Recipe model had a foregin key pointing to Tags model, relating with primary key (id)

2) Querying for specific tags in recipe model.

# RecipeViewSet
# tags.id in []
self.queryset.filter(tags__id__in=[2,3])

3) Reverse mapping of foreign key fields is also possible. Eg, Tag Model does not contain Recipe field. But because of the relation to Tag from Recipe, we can do the following

# TagsViewSet
self.queryset.filter(recipe__isnull=False)

Other filter lookups and their equivalent SQL query

Refer this documentation