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
It always shows me the complete list of user..
What can i do to fix the problem?
Thanks a lot
Agnese Camellini