Home > Software design >  Django ListCreateAPIView not calling get_querryset
Django ListCreateAPIView not calling get_querryset

Time:09-29

Hello I started with Django couple weeks ago, I wanted to return the objects that are closest to the location send in the query parameters. I think that the function get_querryset is never called because it doesn't change anything. These are the files I have:

#models.py
from django.contrib.gis.db import models

class CarOffering(models.Model):
    name = models.CharField(max_length=200)
    location = models.PointField(null=True,blank=True)

    def __str__(self):
        return self.name
#serializer.py
from .models import CarOffering
from rest_framework import serializers

class CarSerializer(serializers.ModelSerializer):
    distance = serializers.FloatField(source='distance.mi', read_only=True, required=False)
    class Meta:
        model = CarOffering
        fields = ['name','location', 'distance']
        read_only_fields = ['location']
#Views.py
from .models import CarOffering
from django.contrib.gis.geos import GEOSGeometry
from django.contrib.gis.db.models.functions import Distance
from .serializers import CarOfferingSerializer

from django.shortcuts import render
from rest_framework.generics import ListCreateAPIView

import geocoder

class ListCreateCarOffering(ListCreateAPIView):
    queryset = CarOffering.objects.all()
    serializer_class = CarOfferingSerializer

    def get_querryset(self):
        qs = super().get_querryset()
        latitude = self.request.query_params.get('lat', None)
        longitude = self.request.query_params.get('lng', None)

        if latitude and longitude:
            pnt = GEOSGeometry('POINT('   str(longitude)   ' '   str(latitude)   ')', srid=4326)
            qs = qs.annotated(distance=Distance('location', pnt)).order_by('distance')

        return qs
#urls.py
from django.contrib import admin
from django.urls import path
from proj.views import ListCreateCarOffering

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/caroffer', ListCreateCarOffering.as_view(),name = 'list_caroffer')

When I look in http://127.0.0.1:8000/api/caroffer I get the same thing as if I do http://127.0.0.1:8000/api/caroffer?lat=0&lng=0:

[
    {
        "name": "test2",
        "location": "SRID=4326;POINT (-0.9024781294996892 37.71827450265065)"
    },
    {
        "name": "test3",
        "location": "SRID=4326;POINT (0 0)"
    }
]

Any idea why I don't get the distance attribute? Thank you!

CodePudding user response:

I'm somewhat new to Django as well, but was able to get this working by defining the "get" method under the ListAPIView, and then working with the queryset & response params from there.

I didn't go through the trouble of installing the dependencies for the Distance expression; nonetheless, the URL dispatch still seemed to run the desired annotate method:

class ListCreateCarOffering(ListCreateAPIView):
    queryset = CarOffering.objects.all()
    serializer_class = CarSerializer

    def get(self, request):
        
        latitude = self.request.query_params.get('lat', None)
        longitude = self.request.query_params.get('lng', None)

        if latitude and longitude:

            pnt = "id"
            qs = self.queryset.annotate(distance=Count(pnt)).order_by('distance')

        return HttpResponse([{'name': q.name, 'location': q.location, 'distance': q.distance} for q in qs])

  • Related