reverse-lookups, strange behaviour

5 views
Skip to first unread message

gabor

unread,
May 17, 2006, 10:11:07 PM5/17/06
to django-d...@googlegroups.com
hi,

i had a problem with reverse-lookups, and found this strange anomaly:

===================
class User(models.Model):
name = models.CharField(maxlength=200)

class Poll(models.Model):
question = models.CharField(maxlength=200)
creator = models.ForeignKey(User)
===================

project name : proj1
app name : app1

this works:

===============
from proj1.app1.models import User
User.objects.get(poll__question__exact='x')
===============

this fails:

===============
from app1.models import User
User.objects.get(poll__question__exact='x')
===============

(fails with : "TypeError: Cannot resolve keyword 'poll' into field",
full stacktrace at http://tinyurl.com/jyp3m)

but this problem only happens with reverse lookups. "normal" lookups
work fine in both cases.

any ideas why this is happening?

i'm not sure if i'm doing something stupid here, or this is a bug.

thanks,
gabor

Jorge Gajon

unread,
May 18, 2006, 11:30:06 PM5/18/06
to django-d...@googlegroups.com
> from proj1.app1.models import User
> User.objects.get(poll__question__exact='x')

> from app1.models import User
> User.objects.get(poll__question__exact='x')


I don't see any difference except for the 'from' statement.
What 'reverse lookup' are you referring to?

Gábor Farkas

unread,
May 19, 2006, 7:15:03 AM5/19/06
to django-d...@googlegroups.com
Jorge Gajon wrote:
>> from proj1.app1.models import User
>> User.objects.get(poll__question__exact='x')
>
>> from app1.models import User
>> User.objects.get(poll__question__exact='x')
>
>
> I don't see any difference except for the 'from' statement.

well, that's it :)

> What 'reverse lookup' are you referring to?


if you look at the model:


> ===================
> class User(models.Model):
> name = models.CharField(maxlength=200)
>
> class Poll(models.Model):
> question = models.CharField(maxlength=200)
> creator = models.ForeignKey(User)
> ===================

the relation goes "from" Poll "to" User.
so a normal lookup would be:

Poll.objects.get('creator__name__exact' = 'gabor')

but i'm doing a "reverse" one.

and the strange thing (the reason why i'm reporting this) is that just
based on the difference in the "from" statement, i get different results
(works in first case, fails in second case).

btw. the "normal" lookup works fine in both cases.

best to try out in "manage.py shell".

gabor

Jorge Gajon

unread,
May 22, 2006, 4:34:07 PM5/22/06
to django-d...@googlegroups.com
On 5/19/06, Gábor Farkas <ga...@nekomancer.net> wrote:
> and the strange thing (the reason why i'm reporting this) is that just
> based on the difference in the "from" statement, i get different results
> (works in first case, fails in second case).
>

You should always use 'from' like this:
from proj1.app1.models import MyModel

Also, I'm recalling from memory so I could be totally wrong here, but
I kind of remember that you would fetch the related set with something
like this:

User.poll_set.get(question__exact="blah")

And you could change the related set name like this:

class Poll(models.Model):
question = models.CharField(maxlength=200)

creator = models.ForeignKey(User, related_name="polls")

And then you could do this:
User.polls.get(question__exact="blah")

Consult the docs because I'm pulling this out of memory.

I normally look into the tests that come in trunk/tests first. They
are very good to find out how to use some feature.

Cheers,
Jorge

gabor

unread,
May 22, 2006, 4:54:56 PM5/22/06
to django-d...@googlegroups.com
Jorge Gajon wrote:
>
> On 5/19/06, Gábor Farkas <ga...@nekomancer.net> wrote:
>> and the strange thing (the reason why i'm reporting this) is that just
>> based on the difference in the "from" statement, i get different results
>> (works in first case, fails in second case).
>>
>
> You should always use 'from' like this:
> from proj1.app1.models import MyModel

i understand that it's safer to do, but quite ugly in my opinion.
why should my code depend on the name of the project, after all..

>
> Also, I'm recalling from memory so I could be totally wrong here, but
> I kind of remember that you would fetch the related set

<snip/>

yes, related_name works like that. but it works (and fails) the same way
as the "unnamed" reverse-lookup.

> I normally look into the tests that come in trunk/tests first. They
> are very good to find out how to use some feature.
>

actually, that was the place where i started (when i saw that my reverse
lookups do not work), and found that the test's reverse lookups do work.

and for some strange reason, this only happens in non-web (manage.py
shell, or "standalone" python script) situations. in "normal" view code
i can use this import without problems.

anyway, it's not such a big problem as such. the problem i think is that
it can lead to confusing situations (the "non-reverse" lookups work fine
in the from-app1.models situation too).


thanks,
gabor

Reply all
Reply to author
Forward
0 new messages