詹戈。一个SQL查询中两点的距离

2024-04-25 06:32:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我的任务是找到离公寓最近的城镇。我想这不是一项困难的任务,但我是新来的,所以每个人都会帮忙:) 我只能使用一个SQL查询。当然要用django ORM。你知道吗

from django.contrib.gis.db.models import MultiPolygonField, PointField
from django.db import models


class City(models.Model):
    geo_area = MultiPolygonField(verbose_name='Area')
    center = PointField(verbose_name='center of City')


class Flat(models.Model):
    geo_point = PointField(verbose_name='Coordinates of apartment')

def distance_from_city(self):
    """
    Method calculating distance to the nearest town. 
    In the case where the coordinates of the apartments are located
    in the middle of the city, it counts the distance to the center 
    of the city (City.center) otherwise has the distance to the border 
    town (City.geo_area) closest to the coordinates of the apartment.
    The distance is returned in kilometers
    """
    pass

Tags: ofthetodjangonamefromcityverbose