3

I have to implement an end-point with Django Rest Framework that receives extra parameters this way:

GET .../hotels_in_period/?check_in=2018-09-30&check_out=2018-10-10

The end-point should receive both parameters check-in and check-out, but I don't know how to get them in the list method of the viewset. I thought they would be available as request.data, but they are not.

I'll appreciate any help.

1 Answer 1

6

Use request.query_params

From the doc

request.query_params is a more correctly named synonym for request.GET.

For clarity inside your code, we recommend using request.query_params instead of the Django's standard request.GET. Doing so will help keep your codebase more correct and obvious - any HTTP method type may include query parameters, not just GET requests

example:

class TagsAPIView(APIView):
    def get(self, request):
        search = request.query_params.get('search')
        if search:
            data = get_paginated_data(
                data=TagSerializer(
                    Tag.objects.filter(name__contains=search).annotate(posts_count=Count('posts')).order_by(
                        '-posts_count').exclude(posts__isnull=True), many=True).data,
                page=request.query_params.get('page'),
                limit=request.query_params.get('limit'),
                url=F"/social/tags/?search={search}"
            )
        else:
            data = get_paginated_data(
                data=TagSerializer(
                    Tag.objects.all().annotate(posts_count=Count('posts')).order_by('-posts_count').exclude(
                        posts__isnull=True),
                    many=True).data,
                page=request.query_params.get('page'),
                limit=request.query_params.get('limit'),
                url=F"/social/tags/?"
            )
        return JsonResponse(data)
Sign up to request clarification or add additional context in comments.

1 Comment

Great! Thanks a lot

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.