views:

43

answers:

1

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));

How would I do this in GeoDjango? It gets a little tiring writing custom SQL everywhere.

+1  A: 

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.

Felix Kling
Well, I am using MYSQL for the raw query....
TIMEX
@alex: Yes but this kind of distance lookup with Django does not work with MySQL. Or you have to extend Django to translate this field lookup into an appropriate SQL statement for MYSQL.
Felix Kling
Do you recommend using PostegreSQL anyway...if I'm working with location?
TIMEX
@alex: You have to set up some things to make it work with PostgreSQL but after that it runs smoothly. Some people say that PostgreSQL is *better* than MySQL but I cannot comment on this. If you want to avoid such custom SQL queries give PostgreSQL a try.
Felix Kling