I have the following model:
class Stop(models.Model):
name = models.CharField(max_length=50)
point = models.PointField()
objects = models.GeoManager()
I want to sort my stops from north to south. How do I do this?
Stop.objects.order_by('point')
sorts them west to east.
thanks.
This is my full model:
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
Since your point field is Geometry and not geography you are in luck.
from django.db.models import F, Func
Stop.objects.annotate(lat=Func('point',function='ST_X')).order_by('lat')
I am using ST_X here instead of ST_Y because PostGIS the best of the geodjango databases the convention is to store lng,lat
If you your point field had been geography the above function call will not have worked because ST_X and ST_Y are only available on geometry fields. Geography fields need to be cast to Geometry before you can use these functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With