#15804: Query lookup types should be scoped to the last joined field's model
-------------------------------------+-------------------------------------
               Reporter:  adrian     |        Owner:  nobody
                   Type:  Bug        |       Status:  new
              Milestone:             |    Component:  Database layer
                Version:  1.3        |  (models, ORM)
             Resolution:             |     Severity:  Normal
           Triage Stage:  Design     |     Keywords:
  decision needed                    |    Has patch:  0
    Needs documentation:  0          |  Needs tests:  0
Patch needs improvement:  0          |
-------------------------------------+-------------------------------------
Description changed by adrian:

Old description:

> This is kind of obscure and best described by example. I have two models:
>
> {{{
> from django.db import models
> from django.contrib.gis.db import models as geo_models
>
> # Note this is a geo-aware model.
> class Place(geo_models.Model):
>     geom = geo_models.GeometryField()
>
> # Note this is NOT a geo-aware model.
> class Person(models.Model):
>     name = models.CharField(max_length=50)
>     hometown = models.ForeignKey(Place)
> }}}
>
> I'd like to be able to do this:
>
> {{{
> Person.objects.filter(hometown__geom__intersects=some_geometry)
> }}}
>
> ...but that doesn't work. It gives me this error:
>
> {{{
> django.core.exceptions.FieldError: Join on field 'geom' not permitted.
> Did you misspell 'intersects' for the lookup type?
> }}}
>
> This happens because {{{Person}}} isn't a geo-aware model, so it doesn't
> know that {{{__intersects}}} is a valid lookup type. I can fix it by
> changing {{{Person}}} to extend {{{django.contrib.gis.db.models}}}, but
> that feels hacky because the {{{Person}}} model itself doesn't have any
> geo fields.
>
> The solution could be to change Django's join code so that it looks at
> the model of the last field in the chain when determining whether a
> lookup is valid. I haven't looked into how difficult this would be,
> though. I think it's just enough of an edge case that it wouldn't be
> worth fixing if it required a ton of refactoring and hoop-jumping. But if
> it's easy, let's do it.

New description:

 This is kind of obscure and best described by example. I have two models:

 {{{
 from django.db import models
 from django.contrib.gis.db import models as geo_models

 # Note this is a geo-aware model.
 class Place(geo_models.Model):
     geom = geo_models.GeometryField()

 # Note this is NOT a geo-aware model.
 class Person(models.Model):
     name = models.CharField(max_length=50)
     hometown = models.ForeignKey(Place)
 }}}

 I'd like to be able to do this:

 {{{
 Person.objects.filter(hometown__geom__intersects=some_geometry)
 }}}

 ...but that doesn't work. It gives me this error:

 {{{
 django.core.exceptions.FieldError: Join on field 'geom' not permitted. Did
 you misspell 'intersects' for the lookup type?
 }}}

 This happens because {{{Person}}} isn't a geo-aware model, so it doesn't
 know that {{{__intersects}}} is a valid lookup type. I can fix it by
 changing {{{Person}}} to extend {{{django.contrib.gis.db.models}}} and
 adding the {{{GeoManager}}}, but that feels hacky because the {{{Person}}}
 model itself doesn't have any geo fields.

 The solution could be to change Django's join code so that it looks at the
 model of the last field in the chain when determining whether a lookup is
 valid. I haven't looked into how difficult this would be, though. I think
 it's just enough of an edge case that it wouldn't be worth fixing if it
 required a ton of refactoring and hoop-jumping. But if it's easy, let's do
 it.

--

-- 
Ticket URL: <http://code.djangoproject.com/ticket/15804#comment:1>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to