Reputation: 33
I want to sort my result based on the distance calculated in the geodjango query.
I tried with two methods but they are throwing errors.
models.py
class Partner(models.Model):
name = models.CharField(max_length=255)
address = models.CharField(max_length=255)
location = models.PointField(u"longitude/latitude",geography=True, blank=True, null=True)
views.py
method 1:
testing = Partner.objects.filter(location__distance_lte=(pnt, D(km=40))).annotate(distance=Distance('location', pnt)).order_by('distance')
print(testing)
Error:
Internal Server Error: /partner/filter/
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/prasanna/Projects/partner_server/partner/views.py", line 79, in Filter_function
print(testing.annotate(distance=Distance('location', pnt)).order_by('distance'))
TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given
method 2:
testing = Partner.objects.filter(location__distance_lte=(pnt, D(km=40))).annotate(distance=Distance('location', pnt)).order_by('distance')
print(testing)
Error:
Internal Server Error: /partner/filter/
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/prasanna/Projects/partner_server/partner/views.py", line 79, in Filter_function
print(testing.distance(pnt).order_by('distance'))
AttributeError: 'QuerySet' object has no attribute 'distance'
Upvotes: 1
Views: 298
Reputation: 1278
Your query is correct but i think you missed something or you mixed between two types of Distance and the first one usage is different from the second one.
this one is a Measurement Object
1- from django.contrib.gis.measure import Distance
and this one is one ofGeographic Database Functions
2-from django.contrib.gis.db.models.functions import Distance
the second one will iterate over every object (row in db) in a comparison manner that the annotate function understands,however the first function is only a basic measurement function that cannot iterate or behave as comparison function
you can refer to django docs and see the difference
Measurement : https://docs.djangoproject.com/en/2.1/ref/contrib/gis/measure/
DB Function : https://docs.djangoproject.com/en/2.1/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.Distance
Upvotes: 1