I have users endpoint working fine, with various FilterBackends. I'm trying to add in a new filter to be able to pass in a list of ids such as ?ids=1,5,7 and returning only those users.
I have found the following filter which accomplishes that, but then breaks my other filters:
class ListFilter(Filter):
def filter(self, qs, value):
if not value:
return qs
self.lookup_type = 'in'
values = value.split(',')
return super(ListFilter, self).filter(qs, values)
class UserListFilter(FilterSet):
ids = ListFilter(name='id')
class Meta:
model = get_user_model()
fields = ['ids']
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
filter_backends = (SearchFilter, DjangoFilterBackend, OrderingFilter, InterestsFilter)
filter_fields = ('username', 'native_language', 'country', 'interests',)
ordering_fields = ('username',)
search_fields = ('first_name', 'last_name')
filter_class = UserListFilter
Before adding in this custom filter_class, all the filter backends work fine, but then after adding in the filter_class, it breaks them all, but I can then filter by a list of ids.
In the filtering docs they give an example using both backends and filter, so I would think this should work, but it's something wrong with my code. Any ideas?