Ahem! You have hijacked an existing thread and your post isn't about
installing on a Mac. I've change the topic, but please start a new
thread next time.

On Mon, 2009-03-02 at 23:28 +0100, Marek Wawrzyczek wrote:
> Hi_
> _
> At: 
> http://docs.djangoproject.com/en/dev/topics/db/queries/#spanning-multi-valued-relationships
>  
> there is written: /
> 
> 
> "That may sound a bit confusing, so hopefully an example will clarify. 
> To select all blogs that contains entries with "Lennon" in the headline 
> and were published in 2008, we would write:
> 
> Blog.objects.filter(entry__headline__contains='Lennon',
>         entry__pub_date__year=2008)
> 
> To select all blogs that contain an entry with "Lennon" in the headline 
> as well as an entry that was published in 2008, we would write:
> 
> Blog.objects.filter(entry__headline__contains='Lennon').filter(
>         entry__pub_date__year=2008)
> 
> In this second example, the first filter restricted the queryset to all 
> those blogs linked to that particular type of entry. The second filter 
> restricted the set of blogs further to those that are also linked to the 
> second type of entry. The entries select by the second filter may or may 
> not be the same as the entries in the first filter. We are filtering the 
> Blog items with each filter statement, not the Entry items.
> "
> 
> /I've got the following questions: how will these two examples will 
> translate into SQL?

If you have a queryset, say "qs", you can look at the SQL that will be
produced by printing out

        qs.query.as_sql()
        
Very useful for debugging and answering curiosity questions like this.

>  I don't understeand why these two may give different 
> results. The second example in my mind should work this way: all blogs 
> that have headline containing 'Lennon' will be filtered to find those 
> whose have at least one entry that has pub_date = 2008.

That is exactly correct. The important thing is that it filters the Blog
set. The first case is different because the initial filtering is for
Blogs containing an Entry that simultaneously satisfies both conditions
(the "entry" attribute inside a single filter() call always refers to
the same entity). I can see that the sentence describing the first case
probably could do with a qualifier as to which object it's referring to
in the "and also..." bit. It means the "and also the entry has a
headline...".

In short, the first queryset is a subset of the results in the second
one, since it's more restricted.

Regards,
Malcolm



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