86

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?

1
  • The modelviewset has a few methods where you can access the REQUEST parameter. Like def get_queryset(self) - self.request, which is probably where you should put your code. Try this and let me know Commented Jan 17, 2018 at 11:10

4 Answers 4

151

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
Sign up to request clarification or add additional context in comments.

6 Comments

This worked but I had to add router.register(r'model_name', views.ModelViewSet , base_name='model_name') in urls.py. However, now POST request gives me the following error: Could not resolve URL for hyperlinked relationship using view name "model-detail". You may have failed to include the related model in your API, or incorrectly configured the lookup_field attribute on this field.
what if i pass a boolean param? self.request.query_params.get returns a string. Is there a clean way to get boolean params? or should i do a string comparison?
@saran3h I do not think that there is a way to directly get a boolean value from query_params, you problably should do string comparison in that case.
request.data does not hold POST data exclusively. If you make a GET request with x-www-urlencoded and use body , request.GET and request.query_params are empty and the data is held in request.data
This is my url path('consignmentdocs/<uuid:pk>/', ConsignmentDocsListViewSet.as_view(), name='consignment-docs'), how do I get the pk using query_params to use in filtering a Model?
|
5

use this simplest way you can get query param values:

request.GET.get("radius")

Comments

4

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.

Comments

0

this actually worked for me . using the self.request.query_params.get("lead_contact_id")

def get_queryset(self,*args,**kwargs):
    # the lead id
    lead_contact_id = self.request.query_params.get("lead_contact_id")
    #  this filter base on the lead id  provided
    feedback = Feedback.objects.filter(object_id=lead_contact_id)
    return feedback

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.