在我正在构建的站点中,我存储了带有城市外键的事件。像这样:
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 中这样做是可能的,但这是我在整个项目中必须做的唯一“地理事情”。我必须毫无理由地使用那个“复杂”的解决方案,还是有办法以更有效的方式做到这一点?