going crazy: foreignkey-reverse-lookup: tests work,my code fails

94 views
Skip to first unread message

gabor

unread,
May 16, 2006, 5:48:05 PM5/16/06
to Django users
hi,

(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

gabor

unread,
May 16, 2006, 6:31:45 PM5/16/06
to django...@googlegroups.com


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

opendev

unread,
Jun 14, 2006, 7:05:36 AM6/14/06
to Django users
I have exactly the same problem, my django is the latest mr head unter
Python 2.4(win32).

Rick

unread,
Jun 14, 2006, 1:51:02 PM6/14/06
to Django users
I'm having a similar (but not identical) problem.

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()

gabor

unread,
Jun 14, 2006, 2:14:11 PM6/14/06
to django...@googlegroups.com
opendev wrote:
> I have exactly the same problem, my django is the latest mr head unter
> Python 2.4(win32)

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

Brett Parker

unread,
Jun 14, 2006, 2:22:42 PM6/14/06
to django...@googlegroups.com
On Wed, Jun 14, 2006 at 10:51:02AM -0700, Rick wrote:
>
> I'm having a similar (but not identical) problem.
>
> 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')
^ should be __ surely?


Cheers,
--
Brett Parker

Malcolm Tredinnick

unread,
Jun 14, 2006, 9:09:09 PM6/14/06
to django...@googlegroups.com
On Wed, 2006-06-14 at 20:14 +0200, gabor wrote:
> opendev wrote:
> > I have exactly the same problem, my django is the latest mr head unter
> > Python 2.4(win32)
>
> 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

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


James Bennett

unread,
Jun 15, 2006, 12:59:28 AM6/15/06
to django...@googlegroups.com
On 6/14/06, Malcolm Tredinnick <mal...@pointy-stick.com> wrote:
> 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.

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

Gábor Farkas

unread,
Jun 15, 2006, 5:17:18 AM6/15/06
to django...@googlegroups.com
James Bennett wrote:
> On 6/14/06, Malcolm Tredinnick <mal...@pointy-stick.com> wrote:
>> 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.
>
> Better to let Guido and company fix relative imports in Python,
> period. Supposed to happen soon, IIRC :)
>

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

Curtis

unread,
Jun 15, 2006, 4:25:19 PM6/15/06
to Django users
I think this may be the same problem that is documented in
http://code.djangoproject.com/ticket/1796

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

Reply all
Reply to author
Forward
0 new messages