Метод расстояния не работает в GeoDjango (QuerySet)
Моя модель:
class City(models.Model):
name_english = models.CharField(max_length=500, null=True, blank=True)
site = models.ForeignKey(Site, on_delete=models.CASCADE, null=True, blank=True, verbose_name="city", db_index=True, related_name="city_relate")
name_city_avito = models.CharField(max_length=500, null=True, blank=True)
country = models.ForeignKey(Country, on_delete=models.CASCADE, null=True, blank=True, verbose_name="Country", db_index=True, related_name="Country_relate")
region = models.ForeignKey(Region, on_delete=models.CASCADE, null=True, blank=True, verbose_name="Region", db_index=True, related_name="Region_relate")
point = models.PointField(blank=True, null=True)
view.py
class Landing_head(View):
def get(self, request):
b = Site.objects.get_current()
current_location = City.objects.get(site__id=b.id).point
cities = City.objects.filter(point__distance_lte=(current_location, D(km=200))).distance(current_location).order_by('point_distance')[:1][0]
for z in cities:
print(z)
ошибка вывода «QuerySet» объект не имеет атрибута «distance»
Хотя в этой версии это работает, без сортировки по расстоянию
class Landing_head(View):
def get(self, request):
b = Site.objects.get_current()
current_location = City.objects.get(site__id=b.id).point
cities = City.objects.filter(point__distance_lte=(current_location, D(km=200)))
for z in cities:
print(z)