I would like to filter a queryset for items within a specific range. This is what my model looks like
class modelEmployee(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
location = models.PointField(srid=4326,max_length=40, blank=True,null=True)
objects = GeoManager()
Now this is how I am running a filter command. To return items within a specific range of 90 miles.
qset = modelEmployee.objects.filter(location__distance_lte=(someLocation, D(mi=90)))
The result returns an item whose distance is actually 223.732 miles which it shouldnt return.
These are the locations of the two items
location A - lat: 47.628641 and long: -117.402997
location B - lat: 47.618337 and long: -122.205341
The distance b/w the two is actually 223.732 miles. I must be filtering it wrong. Any suggestions on where I might be going wrong ?
From docs geo spatial query you should use dwithin
Your example should use it like this:
qset = modelEmployee.objects.filter(location__dwithin=(someLocation, D(mi=90)))
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