Hi all, I am trying to understand spanning multi-valued relationships (as documented here http://docs.djangoproject.com/en/dev/topics/db/queries/#spanning-multi-valued-relationships). More specifically, these paragraphs:
"Everything inside a single filter() call is applied simultaneously to filter out items matching all those requirements. Successive filter() calls further restrict the set of objects, but for multi-valued relations, they apply to any object linked to the primary model, ***not necessarily*** those objects that were selected by an earlier filter() call. [...] 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." To place this into context, here is the problem I'm grappling with. Say I have a model, Entry, with a ManyToManyField, tags. The Tag model contains just a name field. If I want to filter entries by all those entries that have tags 'apples' AND 'oranges', the above would suggest something along the lines of >>> Entry.objects.filter(tags__name='apples', tags__name='oranges') which of course, does not work, as the latter argument overrides the former. So, I was wondering specifically about that "not necessarily" clause. Having experimented briefly in the shell, I've discovered that >>> Entry.objects.filter(tags__name='apples').filter(tags__name='oranges') seems to do what I want. However, I don't quite understand why! If I am understanding the excerpt above, it basically means that if you filter a model on its ManyToManyField, subsequent chained filters may either use the restricted set returned by the former filter; or it may ignore it altogether, and effectively filter as if the former was not there, and thus the result is a union of the two sets of results. Is that correct? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---