0
我有以下模型:定製SQL用來GeoDjango內置於ForignKey
class UserProfile(models.Model):
user = models.OneToOneField(User)
location = models.PointField(blank=True, null=True, srid=CONSTANTS.SRID)
objects = models.GeoManager()
class Item(models.Model):
owner = models.ForeignKey(UserProfile)
objects = models.GeoManager()
現在我需要按距離排序項目的一些觀點:
p = Point(12.5807203, 50.1250706)
Item.objects.all().distance(p, field='owner__location')
但是,這將引發我一個錯誤:
TypeError: ST_Distance output only available on GeometryFields.
現在我不喜歡在這個問題中提出的解決方案,因爲那樣我不會得到距離,我會失去距離。
所以我想我可以做到這一點通過自定義SQL查詢。我知道這個:
UserProfile.objects.distance(p)
會產生這樣的事情:
SELECT (ST_distance_sphere("core_userprofile"."location",ST_GeomFromEWKB('\x0101000020e6100000223fd12b5429294076583c5002104940'::bytea))) AS "distance", "core_userprofile"."id", "core_userprofile"."user_id", "core_userprofile"."verified", "core_userprofile"."avatar_custom", "core_userprofile"."city", "core_userprofile"."location", "core_userprofile"."bio" FROM "core_userprofile"
所以我的問題是:是否有一些簡單的方法如何手動建立這樣的查詢會按距離排序的項目?
謝謝了額外的方法爲我工作最好的.. – 2015-02-15 23:12:19