Opened 13 months ago

Closed 2 months ago

#22352 closed New feature (wontfix)

GeoQuerySet methods: support lookups on reversed o2o relationships as field_name

Reported by: pchiquet Owned by: anonymous
Component: GIS Version: master
Severity: Normal Keywords: geodjango, GeoQuerySet
Cc: mmitar@…, jernej@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no


GeoQuerySet methods, for example distance, are performed on a given geographic field of the model.

The geographic field to use can be specified thanks the field_name argument. field_name can be a field directly in the model or a lookup to a related field in another model (such as, 'address__point'). However lookups with reversed one to one relation are not supported.

For example, with the following model:

class User(Model):

class UserProfile(NamedModel):
    user = models.OneToOneField(User)
    point = models.PointField()

the following spatial operation on User fails:

  User.objects.all().distance(Point(1,1), field_name='userlocation__point')

and raises the following exception:

  Traceback (most recent call last):
    File "django/contrib/gis/db/models/", line 104, in distance
      return self._distance_attribute('distance', geom, **kwargs)
    File "django/contrib/gis/db/models/", line 593, in _distance_attribute
      procedure_args, geo_field = self._spatial_setup(func, field_name=kwargs.get('field_name', None))
    File "django/contrib/gis/db/models/", line 466, in _spatial_setup
      raise TypeError('%s output only available on GeometryFields.' % func)
  TypeError: ST_Distance output only available on GeometryFields.

The lookup is supported only if the foreign key is in the Queryset model. I think it would be nice to support reversed relations too.

Change History (9)

comment:1 Changed 13 months ago by Pierre Chiquet

  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to anonymous
  • Patch needs improvement unset
  • Status changed from new to assigned

comment:3 Changed 11 months ago by bmispelon

  • Triage Stage changed from Unreviewed to Accepted
  • Type changed from Bug to New feature


This seems like a sensible feature to have so I'll move the ticket to "accepted".
The next step is to find someone familiar with the GIS codebase to review your patch and move the ticket to "ready for checkin".


comment:4 Changed 9 months ago by amirrustam

I think the request should also support reverse OneToMany relationships. This Stackoverflow question is relevant:

comment:5 Changed 4 months ago by timgraham

  • Patch needs improvement set

This will require some changes after the _meta refactor branch is merged. Please send a pull request if you can update your patch after that.

comment:6 Changed 4 months ago by mitar

  • Cc mmitar@… added

comment:7 Changed 4 months ago by kostko

  • Cc jernej@… added

comment:8 Changed 4 months ago by mitar

This would be really cool to have. Including the OneToMany support.

comment:9 Changed 2 months ago by claudep

  • Resolution set to wontfix
  • Status changed from assigned to closed

I have tested this with my branch which replaces GeoQuerySet methods by model functions (#24214). And this was not a problem at all, be it with OneToOne or with OneToMany relations. The syntax will be User.objects.all().annotate(dist=distance('userlocation__point', Point(1,1)))

I'll close this one as won't fix, as I think it is not worth working on to-be-deprecated code. The aforementioned branch is waiting for someone knowledgeable with Oracle GIS to write the Oracle support.

Note: See TracTickets for help on using tickets.
Back to Top