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.


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()


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...


in geodjango, as you know ST_Distance operation can use with Geography columns. The error you get is "ST_Distance output only available on GeometryFields" about lack of the geographic field in your model.

The more important think is that if you want to make spatial lookup, GeoManager is required even if the model does not have a geographic field itself, e.g., in the case of a ForeignKey relation to a model with a geographic field.

you can get some information about GeoManager here.


In order to conduct geographic queries, each geographic model requires a GeoManager model manager. This manager allows for the proper SQL construction for geographic queries; thus, without it, all geographic filters will fail. It should also be noted that GeoManager is required even if the model does not have a geographic field itself, e.g., in the case of a ForeignKey relation to a model with a geographic field. For example, if we had an Address model with a ForeignKey to our Zipcode model:

from django.contrib.gis.db import models
from django.contrib.localflavor.us.models import USStateField

class Address(models.Model):
    num = models.IntegerField()
    street = models.CharField(max_length=100)
    city = models.CharField(max_length=100)
    state = USStateField()
    zipcode = models.ForeignKey(Zipcode)
    objects = models.GeoManager()

The geographic manager is needed to do spatial queries on related Zipcode objects, for example:

qs = Address.objects.filter(zipcode__poly__contains='POINT(-104.590948 38.319914)')

i hope it helps you....

  • Thanks for the reply, however I am not sure how this helps. Are you saying my User class requires a GeoManager and if so how do I add one as I am just using the django auth User model – Edward Sparkes May 12 '12 at 6:53
  • You can extend your user with adding geomanger to it. There is an information here about extending User model. – Aragon May 12 '12 at 12:09
  • That link explains how to add a UserProfile which i have as previously explained, my UserProfile model does have a geomanager but i think because i am going from my MyModel class through User to UserProfile it is failing because User does not have a geomanager but i could be wrong. Im sure there must be a way to resolve this. – Edward Sparkes May 12 '12 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.