Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

633
Views
Django Rest Framework, passing parameters with GET request, classed based views

I would like a user to send a GET request to my Django REST API:

127.0.0.1:8000/model/?radius=5&longitude=50&latitude=55.1214

with his longitude/latitude and radius, passed in parameters, and get the queryset using GeoDjango.

For example, currently I have:

class ModelViewSet(viewsets.ModelViewSet):
    queryset = Model.objects.all()

And what I ideally want is:

class ModelViewSet(viewsets.ModelViewSet):
     radius = request.data['radius']
     location = Point(request.data['longitude'],request.data['latitude']
     # filter results by distance using geodjango
     queryset = Model.objects.filer(location__distance_lte=(location, D(m=distance))).distance(location).order_by('distance')

Now a couple of immediate errors:

1) request is not defined - should I use api_view, i.e. the function based view for this?

2) DRF page says that request.data is for POST, PUT and PATCH methods only. How can send parameters with GET?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can override get_queryset method for that purpose. As for query string parameters, you are right, request.data holds POST data, you can get query string params through request.query_params

def get_queryset(self):
    longitude = self.request.query_params.get('longitude')
    latitude= self.request.query_params.get('latitude')
    radius = self.request.query_params.get('radius')

    location = Point(longitude, latitude)

    queryset = Model.objects.filter(location__distance_lte=(location, D(m=distance))).distance(location).order_by('distance')

    return queryset
over 4 years ago · Santiago Trujillo Report

0

I had the same problem, to solve it you can get parameters from url with self.request.parser_context.get('kwargs') under the get_queryset method.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!