Opened 9 years ago

Closed 9 years ago

#25310 closed New feature (wontfix)

GeoManager's distance() method doesn't work with lookups that span multiple relationships

Reported by: David Seddon Owned by: nobody
Component: GIS Version: 1.8
Severity: Normal Keywords: GeoManager, distance
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

It is not currently possible to use the GeoQuerySet's distance() method to make a query that spans multiple relationships. For example:

    from django.contrib.gis.db import models

    class Location(models.Model):
        point = models.PointField()

        objects = models.GeoManager()


    class Office(models.Model):
        location = models.ForeignKey(Location)

        objects = models.GeoManager()


    class Person(models.Model):
        office = models.ForeignKey(Office)

        objects = models.GeoManager()

It is possible to distance query the offices:

    >>> point = Location.objects.first().point
    >>> Office.objects.distance(point, field_name='location__point')
    [<Office 1>, <Office 2>]

But not people:

    >>> Person.objects.distance(point, field_name='office__location__point')
    *** TypeError: ST_Distance output only available on GeometryFields.    

The reason for this is because of the way django.contrib.gis.db.models.query.GeoQuerySet._geo_field()
performs the check - it just looks for fields that are in the model class's ._meta.fields.

(Incidentally, I'm not sure this is the correct error message - actually it hasn't found the field at all, and should probably say so.)

Change History (1)

comment:1 by Tim Graham, 9 years ago

Resolution: wontfix
Status: newclosed

GeoQuerySet.distance() is deprecated in 1.9 and replaced by the Distance database function. Could you please check if this issue is still relevant there and file a new ticket with an updated example, if so? Thanks!

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