[GIS] GeoDjango Order by distance for Model that is linked to User who has a location on their user profile

distancegeodjango

I need to order my query model by distance, however the point field in question is not on the model class but on the user profile and the i am getting an error when attempting the following.

g = geocoders.Google(domain='maps.google.co.uk')
place, (lat, lng) = g.geocode('SW1')  
MyModel.objects.distance(Point(lat, lng), field_name='user__userprofile__location').order_by('distance')

The error i get is "ST_Distance output only available on GeometryFields"

Both MyModel and UserProfile have a GeoManager but obviously User does not.

Filtering works fine using

MyModel.objects.filter(user__userprofile__location__distance_lte=(Point(lat, lng), D(mi=distance)))

But I can't understand why sorting is not working.

Best Answer

Did anyone figure this one out already to the end? Two options I have tried so far: Proxy model

class GeoUser(User):
    class Meta:
        proxy = True
    # Define managers
    objects = models.GeoManager()

add_to_class

class GeoUserManager(models.GeoManager):
    objects = models.GeoManager()


User.add_to_class('objects', GeoUserManager())

But I keep hitting "ST_Distance output only available on GeometryFields. ()" ... Everything is a point and using field_name

queryset=User.objects.filter(userprofile__open=True).distance(pnt, field_name='userprofile__location').order_by('distance'))

Don't know if this could help.

Update 06/10/2012

So I have been further investigating into this and I'm still not quite getting there. When building a queryset and defining the field_name, I receive mentioned error about GeometryFields. I even went so far to update the auth.User in contrib to include the GeoManager, but this did not change anything. When however building the queryset straight on UserProfile and thus not having to define field_name, things Just Work. Don't know if this is a bug? Since I have found some posts on the net that confirm field_name is working...