Object has no attribute 'distance' - GeoDjango

I mean the answer in the question below as I am trying to run a request that gets the closest object to a given point in my django application. My django version is 1.9.2.

How to return record with lowest distance from point using geodjango?

In my opinion, I am going through Zipcode and using GeoPy to get the location and lat / long.

from django.contrib.gis.geos import *
from django.contrib.gis.measure import D
from geopy.geocoders import Nominatim

geolocator = Nominatim()

zipcode = self.request.query_params.get('zipcode', None)
distance_m = 20000

if zipcode is not None:
    location = geolocator.geocode(zipcode, timeout=None)
    origin = Point((location.latitude, location.longitude))

    queryset = Chapter.objects.filter(location__distance_lte=(origin, D(m=distance_m))).distance(origin).order_by('distance')[:1][0] 
    return queryset

      

The Chapter model has a field called location , which is a point field.

from django.contrib.gis.db import models

class Chapter(models.Model):
    name = models.CharField(max_length=500)
    school = models.ForeignKey(School)
    verification = models.CharField(max_length=50)
    address1 = models.TextField()
    address2 = models.TextField(null=True, blank=True)
    city = models.TextField()
    state = models.TextField()
    zipcode = models.CharField(max_length=5)
    location = models.PointField(srid=4326, null=True, blank=True)
    is_school = models.BooleanField(default=False)
    is_association = models.BooleanField(default=False)
    authorized = models.NullBooleanField(default=False)
    stripe_account_active = models.NullBooleanField(default=False)

    def __unicode__(self):
        return self.name

    def get_nickname(self):
        return self.school.nickname

      

I am getting the following error when this is executed:

AttributeError: 'QuerySet' object has no attribute 'distance'

      

Edit -

When I try to get the distance using the alternate method in the questions above, I get another error that makes me think there is something wrong with my model.

point = Chapter.objects.get(name='Chapter of the Year').location
distance_m = 20000

for chapter in Chapter.objects.distance(point):
    print(chapter.name, chapter.distance)

      

Mistake:

'Manager' object has no attribute 'distance'

      

+3


source to share


2 answers


As written on this line:

queryset = Chapter.objects.filter(location__distance_lte = (origin, D(m=distance_m))).distance(origin).order_by('distance')[:1][0] 

      

You order the result as distance , but it is not a field / attribute in the chapter model. I think you want to order location__distance , so the actual filter request should be:

queryset = Chapter.objects.filter(location__distance_lte= (origin, D(m=distance_m))).distance(origin).order_by('location__distance')[:1][0] 

      



Also this line is completely wrong:

for chapter in Chapter.objects.distance(point):

      

Because distance is not using the filter or get method here . It should be something like this:

point = Chapter.objects.get(name='Chapter of the Year')
distance_m = 20000

for chapter in Chapter.objects.filter(id = point.id ):
    print(chapter.name, chapter.location.distance)

      

+1


source


Got it to work



Chapter.objects.filter(location__distance_lte=(origin, D(m=distance_m))).annotate(distance=Distance('location', origin)).order_by('distance')[:1][0] 

      

+3


source







All Articles