django - order_by('-distance') not working as expected -
i trying make database query gives me 20 closest users ordered distance (so closest first) doing follwoing
distance_mi = 100 origin = geosgeometry('srid=4326;'+ 'point('+ str(user_profile.current_location.x)+' '+str(user_profile.current_location.y)+')') close_users = userprofile.objects.exclude(user = user).filter(current_location__distance_lte=(origin, d(mi=distance_mi))).distance(origin).order_by('-distance')[:20]
but returns 3 users (which correct) first user 0 mi away, second 8 mi away , third 0 mi away excepting 8 mi user @ end.
i not sure doing wrong in this.
also, userprofile
has following model
class userprofile(models.model): # field required. user = models.onetoonefield(user) # other fields here current_location = models.pointfield(null = true) seller_current_location = models.pointfield(null = true) objects = models.geomanager() def __unicode__(self): return u'%s' % (self.user.get_full_name())
since userprofile
contains more 1 geographic field, should specify 1 want field_name
argument distance()
.
userprofile.objects.exclude(user=user).filter(current_location__distance_lte=(origin, d(mi=distance_mi))).distance(origin, field_name='current_location').order_by('distance')[:20]
see documentation more details.
Comments
Post a Comment