Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Rest Framework - How do I limit results returned with Geolocation?

I have a model which stores a users location:

[
{
    "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 am trying to achieve 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 with say 1KM.

What is the best way to achieve this?

P.S - The "status" is tied to the normal user model in the django model using a oneToOneField.

like image 752
jwv Avatar asked May 02 '15 23:05

jwv


People also ask

How do you pass extra context data to Serializers in Django REST framework?

In function-based views, we can pass extra context to serializer with “context” parameter with a dictionary. To access the extra context data inside the serializer we can simply access it with “self. context”. From example, to get “exclude_email_list” we just used code 'exclude_email_list = self.

How do I filter Queryset in Django REST framework?

The simplest way to filter the queryset of any view that subclasses GenericAPIView is to override the . get_queryset() method. Overriding this method allows you to customize the queryset returned by the view in a number of different ways.

Can we do pagination for data in REST framework?

REST framework includes support for customizable pagination styles. This allows you to modify how large result sets are split into individual pages of data.

What is throttling in DRF?

Throttling is similar to permissions, in that it determines if a request should be authorized. Throttles indicate a temporary state, and are used to control the rate of requests that clients can make to an API.


2 Answers

What you're looking for is the ability to query/filter by geolocation. Take a look at GeoDjango.

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

like image 170
Dwight Gunning Avatar answered Oct 14 '22 14:10

Dwight Gunning


Two things, you should be using Django's GIS (GeoDjango) features and the GIS plugin for Django REST framework.

GeoDjango will natively work with your database (probably PostGIS) to accurately store and represent the geospatial data. This means you won't have to worry about normalizing locations as they are given to you, and you won't have to manually handle filtering - like finding locations in a radius.

The GIS plugin provides a DistanceToPoint filter that sounds like exactly what you are looking for. You can pass the number of meters in along with the point to use as the center, and it will remove any results that fall outside of that range. This will allow you to use Django REST framework's built-in views and serializers without having to process the querysets and apply the filtering on your own.

like image 25
Kevin Brown-Silva Avatar answered Oct 14 '22 13:10

Kevin Brown-Silva