Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
GeoDjango query with foreign keys
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
mattxbart  
View profile  
 More options Apr 17 2007, 6:36 pm
From: mattxbart <mattxb...@gmail.com>
Date: Tue, 17 Apr 2007 22:36:33 -0000
Local: Tues, Apr 17 2007 6:36 pm
Subject: GeoDjango query with foreign keys
Hey guys, I'm working with the GeoDjango branch and I was wondering if
there are any plans to allow something like I have below:

>>> ParcelSale.objects.filter(parcel__poly__contained=cpa.get_poly_wkt())

"parcel" is a foreign key to the geometry model information:

class Parcel(models.Model,models.GeoMixin):
    gid = models.IntegerField(db_column='gid',primary_key=True)
    apn = models.CharField(db_column='apn',maxlength=10)
    poly = models.PolygonField(db_column='the_geom',srid=4269,
index=True)
    zip = models.CharField(db_column='zip', maxlength=5)
    objects = models.GeoManager()

I can do spatial queries if they are directly between two models with
polygon fields but I can't seem to get at the spatial queries through
foreign keys.

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/site-packages/django_gis/django/db/models/query.py", line
102, in __repr__
    return repr(self._get_data())
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/site-packages/django_gis/django/db/models/query.py", line
470, in _get_data
    self._result_cache = list(self.iterator())
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/site-packages/django_gis/django/db/models/query.py", line
174, in iterator
    select, sql, params = self._get_sql_clause()
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/site-packages/django_gis/django/db/models/query.py", line
484, in _get_sql_clause
    joins2, where2, params2 = self._filters.get_sql(opts)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/site-packages/django_gis/django/db/models/query.py", line
648, in get_sql
    joins2, where2, params2 = val.get_sql(opts)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/site-packages/django_gis/django/db/models/query.py", line
699, in get_sql
    return parse_lookup(self.kwargs.items(), opts)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/site-packages/django_gis/django/db/models/query.py", line
831, in parse_lookup
    joins2, where2, params2 = lookup_inner(path, lookup_type, value,
opts, opts.db_table, None)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/site-packages/django_gis/django/db/models/query.py", line
970, in lookup_inner
    joins2, where2, params2 = lookup_inner(path, lookup_type, value,
new_opts, new_table, join_column)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/site-packages/django_gis/django/db/models/query.py", line
938, in lookup_inner
    raise TypeError, "Cannot resolve keyword '%s' into field" % name
TypeError: Cannot resolve keyword 'poly__contained' into field

Thanks,
Matt


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Matt Bartolome  
View profile  
 More options Apr 17 2007, 6:56 pm
From: "Matt Bartolome" <mattxb...@gmail.com>
Date: Tue, 17 Apr 2007 15:56:34 -0700
Local: Tues, Apr 17 2007 6:56 pm
Subject: Re: GeoDjango query with foreign keys
I wouldn't think this would actually work but I just put the objects =
models.GeoManager() on my model and it now knows what to do with the
spatial queries. Excellent.

On 4/17/07, mattxbart <mattxb...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jbronn  
View profile  
 More options Apr 18 2007, 6:15 pm
From: jbronn <jbr...@gmail.com>
Date: Wed, 18 Apr 2007 22:15:00 -0000
Subject: Re: GeoDjango query with foreign keys
Matt,

That's a good point you brought up: if there's a ForeignKey to a model
with a geographic field, then the model with the ForeignKey requires
GeoManager to construct geographic queries on related objects (even if
the model itself does not have geographic fields).

The GeoManager overrides the regular Manager to return GeoQuerySet
objects (instead of QuerySet).  The GeoQuerySet objects have their own
parse_lookup() function that constructs the SQL for the geographic
queries.  While its almost verbatim from the 'original' parse_lookup()
in django/db/models/query.py, it has a few changes that allow it to
use the PostGIS lookup types.  Thus, when you have no GeoManager, the
regular Manager is used and it has no clue what to do with the
geographic lookup types.  Therefore, GeoManager needs to be used
whenever geographic queries are needed.

I'll try and put something in the GeoDjango FAQ about this.  Thanks
for posting your experience and solution.

-Justin Bronn


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »