0

My URL has 'nutritionist' query parameter, in this case, it will get profiles that has nutritionist with id=1

/profiles?nutritionist=1

When I tried to test this 'filtering' like this:

def test_get_user_belonging_to_a_nutritionist(self):
        response = self.client.get("/profiles?nutritionist=1/",secure=True)

        users = CustomUser.objects.filter(nutritionist=1)
        serializer = CustomUserDetailsSerializer(users, many=True)

        self.assertEqual(response.data, serializer.data)

The response contains HttpResponsePermanentRedirect object, instead of the normal response

This is my Views.py if it helps

class GetProfilesViewSet(generics.ListAPIView):
    serializer_class = CustomUserDetailsSerializer

    def get_queryset(self):
        """
        Optionally restricts the returned purchases to a given user,
        by filtering against a `username` query parameter in the URL.
        """
        queryset = CustomUser.objects.all()
        nutritionist_id = self.request.query_params.get('nutritionist')
        if nutritionist_id is not None:
            queryset = queryset.filter(nutritionist=nutritionist_id)
        return queryset

How can I test this case?

1 Answer 1

1

Query parameters should be added after / and used with ? began.

When there are multiple query parameters, use & concatenation.

So your URL should be written as /profiles/?nutritionist=1.

Finally, it is recommended that query filters(django-filter) be used instead of overriding the get_queryset method.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.