[Answered ]-Want to filter model objects by email, however filtering is not working in Django REST api

1👍

You defined the parameter in the URL, so this is an URL parameter. request.query_params is however not determined by the path, but by the query string [wiki].

You obtain URL parameters through self.kwargs, so:

class GetItemsByEmail(generics.ListAPIView):
    
    def get_queryset(self):
        #    use self.kwargs ↓
        email_items = self.kwargs.get('user_email')
        if email_items is not None:
            return myModel.objects.filter(user_email=email_items)
        else:
            # return some queryset
            pass

Your urls.py should be updated to work with user_email, not id:

url_patterns = [
   path('users/account=<str:user_email>/items', GetItemsByEmail.as_view()),
]

While it is not impossible, it is not very common to have an equal sign in the URL, nor to include an email address, these are typically done through the query string or in case of non-GET requests in the payload of the request or the headers.

Leave a comment