我有以下型号:
class Stop(models.Model):
name = models.CharField(max_length=50)
point = models.PointField()
objects = models.GeoManager()
Run Code Online (Sandbox Code Playgroud)
我想从北到南排序.我该怎么做呢?
Stop.objects.order_by('point')
他们从西向东排序.
谢谢.
这是我的完整模型:
from django.contrib.gis.db import models
from django.core.urlresolvers import reverse
class Stop(models.Model):
gtfs_stop_id = models.IntegerField(db_index=True, unique=True)
name = models.CharField(max_length=50)
point = models.PointField()
parkings = models.GeometryCollectionField(null=True, blank=True)
objects = models.GeoManager()
def get_absolute_url(self):
return reverse('core:stop', args=(self.id,))
def __str__(self):
return self.name
Run Code Online (Sandbox Code Playgroud)
由于您的点数字段是几何而不是地理位置,所以您很幸运.
from django.db.models import F, Func
Stop.objects.annotate(lat=Func('point',function='ST_X')).order_by('lat')
Run Code Online (Sandbox Code Playgroud)
我在这里使用ST_X而不是ST_Y,因为PostGIS是常规存储的最好的geodjango数据库 lng,lat
如果您的点数字段是地理位置,则上述函数调用将无效,因为ST_X和ST_Y仅在几何字段上可用.在使用这些函数之前,需要将地理字段转换为Geometry.
归档时间: |
|
查看次数: |
515 次 |
最近记录: |