If you want to do filtering on multiple fields why don't you use
rest_framework.filters.SearchFilter  as your filter backend and on your
view add

search_fields = [field1, field2, ...]
That's what rest_framework.filters.SearchFilter backend is made for.

On Sat, Jun 6, 2020, 23:14 Agnese Camellini <agnese.camell...@gmail.com>
wrote:

> Maybe the filtering works but i'm writing the wrong address.
> Can someone help?
> Agnese
>
> On Sat, 6 Jun 2020 at 20:46, Agnese Camellini <agnese.camell...@gmail.com>
> wrote:
>
>> Hello to everyone, i have an issue filtering objects in my viewset (the
>> GET method of the user endpoint).
>>
>> The following is the code i used:
>>
>> """CUSTOM FILTERS"""
>> class MultipleFieldLookupMixin(object):
>>     """
>>     Apply this mixin to any view or viewset to get multiple field
>> filtering
>>     based on a `lookup_fields` attribute, instead of the default single
>> field filtering.
>>     """
>>     def get_object(self):
>>         queryset = self.get_queryset()             # Get the base
>> queryset
>>         queryset = self.filter_queryset(queryset)  # Apply any filter
>> backends
>>         f = {}
>>         for field in self.lookup_fields:
>>             if field in self.kwargs.keys(): # Ignore empty fields.
>>                 f[field] = self.kwargs[field]
>>         obj = get_object_or_404(queryset, f)  # Lookup the object
>>         return obj
>>
>>
>> class UserView(MultipleFieldLookupMixin, viewsets.ModelViewSet):
>>     queryset = User.objects.all()
>>     serializer_class = UserSerializer
>>     lookup_fields = ['username', 'pwd', 'token_confirm', 'email',
>> 'token_cng_pwd']
>>
>> I would like to list users optionally on any of these fields..
>>
>> But when i open the url
>>
>> http://localhost/api/v3/user/?username=anything
>>
>> It always shows me the complete list of user..
>>
>> What can i do to fix the problem?
>> Thanks a lot
>> Agnese Camellini
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django REST framework" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-rest-framework+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-rest-framework/5943a785-0094-4749-b62d-6ddb18f56fafo%40googlegroups.com
>> <https://groups.google.com/d/msgid/django-rest-framework/5943a785-0094-4749-b62d-6ddb18f56fafo%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django REST framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-rest-framework+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-rest-framework/CACXuh-R7GbwhtuJXS1oMTjkVghLCKxwr6Hr2-_igKRYygueHsA%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-rest-framework/CACXuh-R7GbwhtuJXS1oMTjkVghLCKxwr6Hr2-_igKRYygueHsA%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-rest-framework/CAHVAm9bw8bCoORTr4n4-FRzPFyhXAH9ocOGB6nObK_jNOAOUqA%40mail.gmail.com.

Reply via email to