Django Rest Framework - How to limit the results returned using Geolocation?

I have a model that stores the location of users:

[ { "url": "http://192.168.0.22:8000/status/1/", "id": 1, "owner": 1, "test_info": "", "created_at": "2015-05-02T07:09:16.535689Z", "updated_at": "2015-05-02T07:09:16.535746Z", "geolocation": null, "jukebox_mode_enabled": false }, { "url": "http://192.168.0.22:8000/status/2/", "id": 2, "owner": 2, "test_info": "", "created_at": "2015-05-02T07:09:24.206959Z", "updated_at": "2015-05-02T07:09:24.207042Z", "geolocation": null, "jukebox_mode_enabled": false }, 

I'm trying to create a system that allows users to query and see who else is nearby, but for security reasons, I would like to limit the results to users, say, 1KM.

What is the best way to achieve this?

PS - "status" is tied to the normal user model in the django model using oneToOneField.

+7
python django django-rest-framework geolocation
source share
2 answers

What you are looking for is the ability to query / filter by geolocation. Check out GeoDjango .

Once you can run the model filter () with a geographic range, then it's just a matter of applying this to your APIView with django-rest-framework.

+2
source share

Two things, you should use the Django GIS (GeoDjango) and GIS plugin functions for the Django REST framework.

GeoDjango will work with your database (possibly PostGIS ) to accurately store and represent geospatial data. This means that you don’t have to worry about normalizing the locations as given to you, and you don’t have to manually filter the descriptors - for example, finding locations in a radius .

The GIS plugin provides a DistanceToPoint filter that sounds exactly the way you are looking. You can transfer the number of meters along with a point for use as a center, and it will delete any results that fall outside this range. This will allow you to use the built-in views and serializers of the Django REST framework without having to process requests and apply filtering yourself.

+1
source share

All Articles