I am using Django Rest Framework and want to filter the queryset in a view based on a certain field name.
The filter values would be null and additionally any other particular value for the field blog_category which is a ForeignKey value. So filter query should be on two query params
class Blog(ListCreateAPIView):
permission_classes = [DjangoCustomModelPermissions]
queryset = Blog.objects.all().select_related("blog_category")
serializer_class = Blog_Serializer
pagination_class = CustomPageNumberPagination
filterset_fields = {'blog_category': ['isnull', 'exact', 'in']}
I have added the lookup fields isnull to filter null values
I have tried the following url requests but they do not work
/blog/?blog_catgeory__in=1,null
/blog/?blog_catgeory__in=1,isnull
/blog/?blog_catgeory__in=1,isnull=true
/blog/?blog_catgeory__in=1&blog_category__isnull=true
How do I get this work?
Edit After answer Javohir Elmurodov's answer
I made the following modifications but it still does not work
class NumberInFilter(django_filters.BaseInFilter, django_filters.NumberFilter):
pass
class BlogFilter(filters.FilterSet):
blog_category = NumberInFilter(field_name='blog_category', lookup_expr='in')
blog_category__isnull = BooleanFilter(
field_name='blog_category', lookup_expr='isnull')
class Blog_ListView(ListData, CreateData, CustomGenericAPIView):
permission_classes = [DjangoCustomModelPermissions]
queryset = Blog.objects.all().select_related("blog_category")
serializer_class = Blog_Serializer
pagination_class = CustomPageNumberPagination
filterset_class = BlogFilter
I tried the following links
/blog/?blog_catgeory=1&blog_category__isnull
/blog/?blog_catgeory=1&blog_category__isnull=true
Still does not work
I want to search for all entries in queryset that are null OR a particular value in blog_category
/blog/?blog_catgeory__in=1&blog_category__isnull?blog_catgeory__in=1blog_categorynull Blogs. Is Foreign keynull=Trueblog_category__isnull = BooleanFilter( field_name='blog_category', lookup_expr='isnull')toblog_category_isnull = BooleanFilter( field_name='blog_category', lookup_expr='isnull'), because it conflict with autofilter.