3

Using GeoDjango, 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?

Edit

The list seems sorted, but only messes up on for sellers in a certain part of town (its totally crazy!)

  • Can you post some of the data that's messing up, as well as the query point? Also check that you have your coordinates the right way round, it should be POINT(lon lat) – MerseyViking Jul 30 '14 at 11:01
  • I think it would be good to help locate the problem if you can put some real data in your question, for example, the origin coordinates, and query results coordinates and their distance to origin. – mfdev Aug 3 '14 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.