Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

434
Views
django filter for NOT IN as lookup_expr

We can make the django filter with "in" expression sending comma separated string. Such as

import django_filters

class NumberInFilter(django_filters.BaseInFilter, django_filters.NumberFilter):
    pass

class BookFilter(django_filters.FilterSet):
    author = NumberInFilter(field_name="author__id", lookup_expr="in")

However, I was looking for way to send comma separated query-data and get response which do not have the query-data. something like

class BookFilter(django_filters.FilterSet):
    author = NumberInFilter(field_name="author__id", lookup_expr="not_in")

Definitely there is nothing like "not_in". Can you please give me a solution to achieve this?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I'm not sure there is a simple built-in for doing an exlusive lookup in django-filters. However you can probably do this pretty easily with a custom method on your filterset class with a .exclude() :

class BookFilter(django_filters.FilterSet):
    author = django_filters.NumberFilter(method='filter_author')

    def filter_author(self, queryset, name, value):
        return queryset.exclude(author_id__in=value)
over 4 years ago · Santiago Trujillo Report

0

Just wanted to add what I finally got,

class BookFilter(django_filters.FilterSet):
    author = django_filters.NumberFilter(method='filter_author')

    def filter_author(self, queryset, name, value):
        if value:
            return queryset.exclude(author__id__in=value.split(','))
        return queryset
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!