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
> 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?
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
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
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