1

I am trying to sort a list of users based on the distance from a point. It does generate a list, but its not ordered by distance, though I specify that in my query.

This is the query I am using.

origin = GEOSGeometry('SRID=4326;'+ 'POINT('+ str(buyer.buyer_current_location.x)+' '+str(buyer.buyer_current_location.y)+')')
close_sellers = Seller.objects.exclude(profile__user = user).filter(seller_current_location__distance_lte=(origin, D(mi=distance_mi)),profile__user__is_active = True).distance(origin, field_name='seller_current_location').order_by('distance')[:20]

and these are the models that I have

class Buyer(models.Model):
    # This field is required.
    profile = models.ForeignKey(UserProfile,unique=True)
    # Other fields here
    screen_name = models.CharField(max_length = 200,default='buyer')
    buyer_current_location = models.PointField(null = True, srid=4326)
    objects = models.GeoManager()

class Seller(models.Model):
    # This field is required.
    profile = models.ForeignKey(UserProfile,unique=True)
    # Other fields here
    seller_current_location = models.PointField(null = True, srid=4326)
    objects = models.GeoManager()

Does anyone have any clue regarding what might be going wrong?

Jonathan
  • 2,728
  • 10
  • 43
  • 73

2 Answers2

1

Your code is absolutely correct, I've tested it on the project I am working on, and it filters and orders as expected. So the problem has to be somewhere else. A few notes:

  • I hope you are using PostgreSQL with PostGIS, since "extended" Well-Known Text is specific only to PostGIS
  • first line could be replaced by: origin = GEOSGeometry(buyer.buyer_current_location.ewkt) which would give the same result, or even better, simply: origin = buyer.buyer_current_location
  • You don't need to specify srid=4326, it is used by default
lehins
  • 9,642
  • 2
  • 35
  • 49
  • This is strange, as soon as I changed it to origin = buyer.buyer_current_location it started working just fine. I wonder that could've went wrong in the way I was doing it at first. Thank you so much Alexey! – Jonathan Jun 21 '14 at 17:06
  • Alexey actually, that did not fix it. – Jonathan Jun 29 '14 at 01:57
  • and the strangest thing is that when I change distance_mi to 1 miles sill it shows me the fields that are outside 1 mile radius – Jonathan Jun 29 '14 at 03:48
  • Are you sure you have everything configured properly? Your code in the question is correct, it seems that geospatial queries are not working. Have you created a spatial database? https://docs.djangoproject.com/en/1.6/ref/contrib/gis/tutorial/#create-a-spatial-database – lehins Jul 01 '14 at 03:01
  • Alexey, yes I double checked. I created the database and added it in DATABASES['default'] also added 'django.contrib.gis' in installed apps. – Jonathan Jul 01 '14 at 14:47
  • What is the version of PostgreSQL and PostGIS you are using? – lehins Jul 01 '14 at 18:31
  • Also, did you set the proper backend? `'ENGINE': 'django.contrib.gis.db.backends.postgis'` – lehins Jul 01 '14 at 18:35
0

From django source code, order_by is a method which takes items that are either field names possibly with a direction prefix ('-' or '?') or ordinals, and it doesn't take method as parameter, hence you didn't get any ordered list. for details, you can check documentation , even better sourcecode.

ruddra
  • 50,746
  • 7
  • 78
  • 101
  • ruddra, distance is what goes in order_by in this case, please look at this http://stackoverflow.com/questions/19703975/django-sort-by-distance – Jonathan Jun 16 '14 at 16:51
  • I see. Well you are right. I have searched and found more references about order_by distance, and its a geodjango feature. Sorry. – ruddra Jun 16 '14 at 16:57