I have an Event model, that, for the sake of simplicity only has a lat and lon FloatField().
From pythons native "geopy.distance" library, you can acquire the distance bewteen two coordinates like so:
from geopy.distance import distance
home = (33.28473, -117.20384)
event = (33.829384, -117.38932)
distance = distance(home, event).miles #returns a float
In my application context I need to order all of the events by location. I attempted standard django annotation syntax:
Event.objects.all().annotate(distance=distance( (F('lat'),F('lon')), home ).miles).order_by('distance')
But I get the following error from the geopy library:
TypeError: float() argument must be a string or a number, not 'F'
Strangely enough, this works:
>>> def foo(x):
return x * 2
>>> Event.objects.all().annotate(cutomField=foo(F('field')))
It seems that you can do arthmetic with F types but geopy does not seem to know this.
Is there anyway to turn an F type into a float so that geopy can process the field?