Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass query with url in Django REST Framework

I feel like this should be simple, but what I'm doing isn't paying off.

In Django I have this urls.py in my app:

from django.urls import path

from .views import PostcodessAPIListView, PostcodesAPIID, PostcodesWithinRadius

urlpatterns = [
    path("<str:postcode>/<int:radius_km>", PostcodesWithinRadius.as_view(), name="results_api_radius"),
    path("", PostcodessAPIListView.as_view(), name="results_api_list"),
    path("<int:pk>", PostcodesAPIID.as_view(), name="results_api_detail"),
]

And I have this view that should take a postcode and a radius in km, call an external api to convert the postcode to longitude and latitude, and then call the external api again to get postcodes within a certain radius.

class PostcodesWithinRadius(generics.RetrieveAPIView):
    serializer_class = PostcodeSerializer

    def get_queryset(self):
        postcode = self.request.query_params.get('postcode', None)
        radius_km = self.request.query_params.get('radius_km', None)
        print(postcode, radius_km)
        postcodes = self.get_postcodes_within_radius(postcode, radius_km)
        print(postcodes)
        return Postcode.objects.filter(postcode__in=postcodes)

    def get_postcodes_within_radius(self, postcode, radius_km):

        radius_km = 3
        postcodes_within_radius = []

        if radius_km <= 20:
            radius = radius_km * 1000
        else:
            raise ValueError('Radius cannot be over 20km.')

        GET_LAT_LON_URL = f"{POSTCODE_URL}?"

        postcode_query = {
            "query": postcode
        }

        postcode_data = requests.get(GET_LAT_LON_URL, headers=None, params=postcode_query).json()
        print(postcode_data)
        querystring = {
            "longitude": postcode_data['result'][0]['longitude'],
            "latitude": postcode_data['result'][0]['latitude'],
            "wideSearch": radius,
            "limit": 100,
        }

        res = requests.get(POSTCODE_URL, headers=None, params=querystring).json()


        for postcode in res['result']:
            postcodes_within_radius.append(postcode['postcode'])
        return postcodes_within_radius

However, the postcode and radius are not being passed through the url params - what gives?

like image 394
Davtho1983 Avatar asked Apr 14 '19 15:04

Davtho1983


2 Answers

You are not passing postcode and radius as query parameters, but rather as url parameters.

Your method would work if your url was something like:

http://localhost:8000/postcodes/?postcode=0000&radius=5

But since your are using url parameters:

http://localhost:8000/<postcode>/<radius>

You will need to change your get_queryset method. Try this:

def get_queryset(self):
    postcode = self.kwargs.get('postcode', None)
    radius_km = self.kwargs.get('radius_km', None)
    print(postcode, radius_km)
    postcodes = self.get_postcodes_within_radius(postcode, radius_km)
    print(postcodes)
    return Postcode.objects.filter(postcode__in=postcodes)
like image 187
drec4s Avatar answered Nov 04 '22 00:11

drec4s


why dont you just use the get() method of

class PostcodesWithinRadius(generics.RetrieveAPIView):
    serializer_class = PostcodeSerializer

    def get(self, request, **kwargs):
        postcode = kwargs.get('postcode')
        radius_km = kwargs.get('radius_km')
        # rest of the logic 
like image 44
doniyor Avatar answered Nov 04 '22 00:11

doniyor