Problem filtering objects in list viewset with custom mixin

48 views
Skip to first unread message

Agnese Camellini

unread,
Jun 6, 2020, 2:46:14 PM6/6/20
to Django REST framework
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

Agnese Camellini

unread,
Jun 6, 2020, 4:14:33 PM6/6/20
to django-res...@googlegroups.com
Maybe the filtering works but i'm writing the wrong address.
Can someone help?
Agnese

--
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-fram...@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.

Yezileli Ilomo

unread,
Jun 8, 2020, 8:14:14 PM6/8/20
to django-res...@googlegroups.com
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.

Adailton Nascimento

unread,
Jun 9, 2020, 10:25:56 AM6/9/20
to Django REST framework
Firstly if you wanna use your custom mixin instead of drf filter backends you will need to do the following.

you are trying to query query param using kwargs it's wrong, instead use self.query_params. and your f is a dict()

def get_queryset(self):
    f
= dict()
   
for field in self.lookup_fields:
        f
.update({field: self.request.query_params.get(field)})
   
return # do what you want, remember, it's possible to receive more than one query param from query params, so get_object_or_404 won't work if you pass more than one param.


PS: self.query_params return a dict, so you don't need to use lookup fields instead do ...
def get_queryset(self):
    query_params
= self.request.query_params
   
return User.objects.filter(**query_params)


PS2:I highly recommend to use drf filter backend.

PS3: a personal tip try to use an idle that allows you to see the source code of the framework you are using. and try to adhere to the use of pdb debugging things it is much easier to identify what you want or just use print. tip: print('>>>>>', self.query_params). :D

Agnese Camellini

unread,
Jun 9, 2020, 10:30:22 AM6/9/20
to django-res...@googlegroups.com
Thanks a lot Adailton
.
I have figured out by myself di use wury_params but i've made the action more static, for security reasons.
Thanks a lot however.
Agnese

--
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-fram...@googlegroups.com.

Adailton Nascimento

unread,
Jun 10, 2020, 1:39:38 PM6/10/20
to django-res...@googlegroups.com
You are welcome
Happy that you solve your problem.

Adailton do Nascimento
(11) 95493-7660
Software Engineer


   Linkedin github



Reply all
Reply to author
Forward
0 new messages