(from-svn (today) django), postgres
reverse-lookups on foreign keys does not work for me.
i even created a new empty project, and copied the example models from:
http://www.djangoproject.com/documentation/db_api/
into it, and tried the example reverse-query, and it did not work :-(
then i tried to run the tests in django,and they went ok.
even the reverse-lookup test :-(
again, copying the models.py used in that test into a new empty project
does not work, i still get the error.
for example, with the reverse_lookup test models, this query fails:
>>> User.objects.get(poll__question__exact="What's the first question?")
Traceback (most recent call last):
File "<console>", line 1, in ?
File "/Users/gabor/src/django/django/db/models/manager.py", line 66,
in get
return self.get_query_set().get(*args, **kwargs)
File "/Users/gabor/src/django/django/db/models/query.py", line 198,
in get
obj_list = list(clone)
File "/Users/gabor/src/django/django/db/models/query.py", line 94, in
__iter__
return iter(self._get_data())
File "/Users/gabor/src/django/django/db/models/query.py", line 383,
in _get_data
self._result_cache = list(self.iterator())
File "/Users/gabor/src/django/django/db/models/query.py", line 159,
in iterator
select, sql, params = self._get_sql_clause()
File "/Users/gabor/src/django/django/db/models/query.py", line 397,
in _get_sql_clause
tables2, joins2, where2, params2 = self._filters.get_sql(opts)
File "/Users/gabor/src/django/django/db/models/query.py", line 528,
in get_sql
tables2, joins2, where2, params2 = val.get_sql(opts)
File "/Users/gabor/src/django/django/db/models/query.py", line 577,
in get_sql
return parse_lookup(self.kwargs.items(), opts)
File "/Users/gabor/src/django/django/db/models/query.py", line 682,
in parse_lookup
tables2, joins2, where2, params2 = lookup_inner(path, clause,
value, opts, opts.db_table, None)
File "/Users/gabor/src/django/django/db/models/query.py", line 785,
in lookup_inner
raise TypeError, "Cannot resolve keyword '%s' into field" % name
TypeError: Cannot resolve keyword 'poll' into field
as i mentioned, when this query is executed in the test, it works fine.
any ideas what am i doing wrong? (except maybe trying to make this work
at 23:46 :-))
thanks,
gabor
maybe this addition info helps:
the model instances do have the reverse-"mapping".
for example this works ok:
u = User.objects.all()[0]
u.poll_set.all()
but as i said, this not:
User.objects.get(poll__question__exact="What's the first question?")
gabor
In my app I have Rate's that belong to a Rateset, and Card's that use a
Rateset. So both Rate and Card have a foreign key connection to
Rateset.
My application needs a list of "active" rates (those rates that are in
use by an active Card). Here is my query:
Rate.objects.filter(rateset__card__active_exact=True).distinct().values('countryfrom')
I'm trying to follow the relationship from Rate, "forward" to Rateset
and "reverse" to Card. I get:
AttributeError: 'RelatedObject' object has no attribute 'column'
Any idea of what's wrong?
Thanks, Rick
<<<< Model >>>>>
class RateSet(models.Model):
description = models.TextField(maxlength=128)
class Continent(models.Model):
id = models.TextField(maxlength=2, primary_key=True)
description = models.TextField(maxlength=40)
class Country(models.Model):
id = models.TextField(maxlength=3, primary_key=True)
continent = models.ForeignKey(Continent)
description = models.TextField(maxlength=40)
class Rate(models.Model):
rateset = models.ForeignKey(RateSet)
countryfrom = models.ForeignKey(Country,
related_name='countryfrom_set')
countryto = models.ForeignKey(Country,
related_name='countryto_set')
perminute = models.FloatField(max_digits=6, decimal_places=4)
class Card(models.Model):
rateset = models.ForeignKey(RateSet)
name = models.CharField(maxlength=24)
active = models.BooleanField()
ah, sorry, i forgot to post my findings here...
basically the "problem" was that i specified the imports "relatively".
example:
1. django-admin.py startproject myproject
2. manage.py startapp myapp
3. now create a model in myapp. let's call it MyModel
at this point, when i create a script to work with my models and import
the model, things can go wrong.
with this import, reverse lookups work:
from myproject.myapp.models import MyModel
with this import, revers lookups fail:
from myapp.models import MyModel
so basically, when importing, also specify the project-name.
in more detail here:
http://groups.google.com/group/django-developers/browse_thread/thread/51ebb62cc40ff3f7
for me this:
1. seems to be a bug in django
2. only happens with my own scripts. if i am running the django app
using the development server, both kind of imports work ok.
gabor
Cheers,
--
Brett Parker
Yeah, sorry about that. We really need to fix this. :-(
But it's not entirely trivial from what I remember last time I dived in
there.
Malcolm
Better to let Guido and company fix relative imports in Python,
period. Supposed to happen soon, IIRC :)
--
"May the forces of evil become confused on the way to your house."
-- George Carlin
i don't think it's a problem with relative/absolute imports :-(
iirc the problem here is that when you import something from 2 different
places, then they are 2 different things ... or something like that. it
was a long time ago that i checked that.
gabor
Obviously, making sure that you for e.g. import from 'myproj' is the
right way, but I did submit a patch for management.py that seems to fix
the problem (while not really addressing the root cause)
Cheers,
-Curt