On Tue, Jul 14, 2009 at 10:31 AM, Streamweaver<streamwea...@gmail.com> wrote:

>> For example I have a model called Project with a ForeignKey to a
>> Django User.
>>
>> class Project(models.Model):
>>    ...
>>     owner = models.ForeignKey(User)
>>   ...
>>
>> by the documentation I would expect the following to give me a list of
>> all Users who are owners of projects but it does not.
>>
>> User.objects.filter(project__owner__isnull=False)
>>
>> Instead it's returning a queryset of all users.
>>
>> Anyone have any insight into this?
...
> Filed a bug report about this.  We'll see if they accept it.

I'm not sure who "they" are supposed to be, but "they" won't accept
this ticket, because this isn't a bug.

The query:

User.objects.filter(project__owner__isnull=False)

Querying on project__owner is essentially asking Django to traverse to
the project table so that it can query an attribute that is already
present on the user table - the ID. Django optimizes this query, and
doesn't do the join, since it already has the data locally. So, your
reduces to 'User.objects.filter(id__isnull=False)' - which is the same
as "User.objects.all()"

What you need to do is query for the non-existence of a project
associated with a user:

User.objects.filter(project__isnull=False)

Yours,
Russ Magee %-)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to