Skip to content
This repository has been archived by the owner on Jun 26, 2019. It is now read-only.

Commit

Permalink
Add distance annotation in NearestItemsFilter queryset
Browse files Browse the repository at this point in the history
  • Loading branch information
k0t3n committed Nov 10, 2018
1 parent 2a39b17 commit 8fdfe9d
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/apps/withdrawal_point/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import coreapi
from django.contrib.gis.geos import Point
from django.contrib.gis.measure import Distance
from django.contrib.gis.db.models.functions import Distance as DistanceFunc
from django.db.models import Q
from django_filters import rest_framework as filters
from rest_framework.exceptions import ParseError
Expand Down Expand Up @@ -42,7 +43,11 @@ def filter_queryset(self, request, queryset, view):
except TypeError:
raise ParseError('Invalid distance string supplied for parameter {0}'.format(self.radius_param))

return queryset.filter(Q(**{'{}__distance_lt'.format(filter_field): (point, dist)}))
return queryset.filter(
Q(**{'{}__distance_lt'.format(filter_field): (point, dist)})
).annotate(
distance=DistanceFunc('coordinates', Point(point.x, point.y, srid=4326))
)

def get_schema_fields(self, view):
return [
Expand Down

0 comments on commit 8fdfe9d

Please sign in to comment.