Opened 11 years ago

Closed 10 years ago

Last modified 9 years 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: dev
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

Description

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):
    pass

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/query.py", line 104, in distance
      return self._distance_attribute('distance', geom, **kwargs)
    File "django/contrib/gis/db/models/query.py", 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/query.py", 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 (17)

comment:1 by Pierre Chiquet, 11 years ago

Owner: changed from nobody to anonymous
Status: newassigned

comment:3 by Baptiste Mispelon, 11 years ago

Triage Stage: UnreviewedAccepted
Type: BugNew feature

Hi,

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

Thanks.

comment:4 by amirrustam, 10 years ago

I think the request should also support reverse OneToMany relationships. This Stackoverflow question is relevant: http://stackoverflow.com/questions/25047964/geodjango-geoqueryset-distance-results-in-st-distance-output-only-available-o

comment:5 by Tim Graham, 10 years ago

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 by Mitar, 10 years ago

Cc: mmitar@… added

comment:7 by Jernej Kos, 10 years ago

Cc: jernej@… added

comment:8 by Mitar, 10 years ago

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

comment:9 by Claude Paroz, 10 years ago

Resolution: wontfix
Status: assignedclosed

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.

comment:10 by artscoop, 10 years ago

The model functions are to be introduced in Django 1.9, and the to-be-deprecated code is still present in Django 1.8 (with LTS). I'm convinced a wontfix is not a good move here.

comment:11 by Claude Paroz, 10 years ago

Sorry, but unless this is a regression, it has no chance to enter Django 1.8.

comment:12 by Claude Paroz, 10 years ago

Also note that if you really need this functionality, it might be possible to adapt [d9ff5ef36d3f714736d633435d45f03eac9c17b5] to make it a third-party app usable from Django 1.8.

comment:13 by artscoop, 10 years ago

This may not be a regression, but I'm sure it's a bug. The field_name exposes the possibility to span across relationships. It works with ForeignKeys, but not with OneToOneFields, which does not make much sense. (The bug occurs because of the way the "field__field__etc" relationship span is processed).
If a fix won't make it to Django 1.8 (there is a fix here: https://github.com/django/django/compare/master...pchiquet:ticket_22352) I can still have a look at the dev version of Django.

Version 2, edited 10 years ago by artscoop (previous) (next) (diff)

comment:14 by Tim Graham, 10 years ago

If it's not a regression from a previous release or a major bug in a new feature it won't be backported. See our supported versions policy.

comment:15 by Arthur Pemberton, 9 years ago

I see this is marked as wontfix, but there seems to be no intermediate solution. Do OneToOne relationships simply not work with GIS in Django?

in reply to:  9 comment:16 by Arthur Pemberton, 9 years ago

Replying to claudep:

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.

So what do we about this in Django 1.8?

comment:17 by Tim Graham, 9 years ago

See comments twelve and fourteen.

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