在我建设的网站中,我用城市的外键存储了事件。就像这样:有必要使用GeoDjango来查询Django中的距离吗?
class Event(models.Model):
name = models.CharField(max_length=255)
...
ciudad = models.ForeignKey(City)
class City(models.Model):
name = models.CharField(max_length=500)
...
lat = models.FloatField()
lon = models.FloatField()
我想在的一些城市一些公里到查询事件。 我实际上做的是:
# isInRange takes two cities and a distance in kms and calculates
# if the distance between the cities (by the fields lat and lon and
# one nice formula) is minor than the given distance.
results = []
for event in Event.objects.all():
if isInRange(city, event.city, kms):
results.append(event)
我知道,是非常低效的。我知道GeoDjango可以做到这一点,但这是我在整个项目中必须做的唯一“地理事物”。我必须毫无理由地使用那种“复杂”的解决方案,或者有办法以更高效的方式来做到这一点?
很好的答案!我明白错误的事情。这是因为距离应该是一个圆形,这会计算一个伪矩形,不是吗?无论如何,我会使用这种方法。如果我注意到错误太多,我将切换到GeoDjango。再次,很好的回答和谢谢! – sanfilippopablo 2013-03-05 18:02:18