1👍
✅
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)
👤rob
3👍
Seems there is a more optimal solution. You can use the NumberFilters’ exclude=True
option like this:
class NumberInFilter(django_filters.BaseInFilter, django_filters.NumberFilter):
pass
class BookFilter(django_filters.FilterSet):
author = NumberInFilter(
field_name="author__id",
lookup_expr="in",
exclude=True
)
- [Django]-Shortening HTML content python / django
- [Django]-Django Session Variable resetting
- [Django]-Understanding Django model inheritance quirks
- [Django]-How can I omit time at HTML {{ post.published_date }} tag?
- [Django]-Receiving ajax posted object in django view
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
- [Django]-Count the number of posts by a user – django
- [Django]-Django Rest app not using media root correctly to serve images
Source:stackexchange.com