开发者

How would I write this query in GeoDjango? (It's a library for geographic calculations in Django)

Right now I'm using raw SQL to find people within 500 meters of the current user.

cursor.execute("SELECT user_id FROM myapp_location WHERE\
       GLength(LineStringFromWKB(LineString(asbinary(utm), asbinary(PointFromWKB(point(%s, %s)))))) < %s"\
       ,(user_utm_easting, user_utm_northing, 500));

H开发者_如何学Pythonow would I do this in GeoDjango? It gets a little tiring writing custom SQL everywhere.


Well assuming you have the appropriate model,

from django.contrib.gis.db import models

class User(models.Model):
    location = models.PointField()
    objects = models.GeoManager()

it would look like:

User.objects.filter(location__dwithin=(current_user.location, D(m=500)))

But note that such distance lookups are not supported by MySQL.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜