对象没有属性 'distance' - GeoDjango
Object has no attribute 'distance' - GeoDjango
我指的是下面列出的问题的答案,因为我正在尝试 运行 一个查询,该查询获取最接近我的 Django 应用程序中给定点的对象。我的 django 版本是 1.9.2.
How to return a record with the lowest distance from a point using geodjango?
在我看来,我正在传递邮政编码并使用 GeoPy 获取位置和 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
Chapter 模型有一个名为 location 的字段,它是一个点字段。
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
执行时出现以下错误:
AttributeError: 'QuerySet' object has no attribute 'distance'
编辑--
当我尝试使用上述问题中的替代方法获取距离时,我得到了一个不同的错误,这让我觉得我的模型有问题。
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)
错误:
'Manager' object has no attribute 'distance'
如这一行所写:
queryset = Chapter.objects.filter(location__distance_lte = (origin, D(m=distance_m))).distance(origin).order_by('distance')[:1][0]
您按 距离 对结果进行排序,但这不是章节模型中的 field/attribute。
我想你想要的是按location__distance排序,所以实际的过滤查询应该是:
queryset = Chapter.objects.filter(location__distance_lte= (origin, D(m=distance_m))).distance(origin).order_by('location__distance')[:1][0]
另外这一行完全不正确:
for chapter in Chapter.objects.distance(point):
因为这里的distance不是像filter或get那样的方法。
应该是这样的:
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)
成功了
Chapter.objects.filter(location__distance_lte=(origin, D(m=distance_m))).annotate(distance=Distance('location', origin)).order_by('distance')[:1][0]
我指的是下面列出的问题的答案,因为我正在尝试 运行 一个查询,该查询获取最接近我的 Django 应用程序中给定点的对象。我的 django 版本是 1.9.2.
How to return a record with the lowest distance from a point using geodjango?
在我看来,我正在传递邮政编码并使用 GeoPy 获取位置和 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
Chapter 模型有一个名为 location 的字段,它是一个点字段。
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
执行时出现以下错误:
AttributeError: 'QuerySet' object has no attribute 'distance'
编辑--
当我尝试使用上述问题中的替代方法获取距离时,我得到了一个不同的错误,这让我觉得我的模型有问题。
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)
错误:
'Manager' object has no attribute 'distance'
如这一行所写:
queryset = Chapter.objects.filter(location__distance_lte = (origin, D(m=distance_m))).distance(origin).order_by('distance')[:1][0]
您按 距离 对结果进行排序,但这不是章节模型中的 field/attribute。 我想你想要的是按location__distance排序,所以实际的过滤查询应该是:
queryset = Chapter.objects.filter(location__distance_lte= (origin, D(m=distance_m))).distance(origin).order_by('location__distance')[:1][0]
另外这一行完全不正确:
for chapter in Chapter.objects.distance(point):
因为这里的distance不是像filter或get那样的方法。 应该是这样的:
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)
成功了
Chapter.objects.filter(location__distance_lte=(origin, D(m=distance_m))).annotate(distance=Distance('location', origin)).order_by('distance')[:1][0]