On Wed, 2007-08-15 at 06:06 +0000, [EMAIL PROTECTED] wrote: > Hi, > > I've think I've found a bug with complex queries and QOr. Here is the > test case model I've reduced it to: > > from django.db import models > > class Author(models.Model): > name = models.CharField(max_length=100) > > def __str__(self): > return self.name > > class Article(models.Model): > title = models.CharField(max_length=100) > author = models.ForeignKey(Author, blank=True, null=True) > > def __str__(self): > return self.title > > > and here is the test code to reproduce the problem: > > >>> from qorbug.models import Author, Article > >>> from django.db.models import Q > >>> author = Author.objects.create(name="John Doe") > >>> Article.objects.create(title="Article One", author=author) > <Article: Article One> > >>> Article.objects.create(title="Article Two") > <Article: Article Two> > >>> Article.objects.filter(Q(title__icontains="article") | > >>> Q(author__name__icontains="article")) > [<Article: Article One>] > > For that last query I was expecting: > >>> Article.objects.filter(Q(title__icontains="article") | > >>> Q(author__name__icontains="article")) > [<Article: Article One>, <Article: Article Two>] > > as both Articles have "article" in their title. > > Am I wrong to expect that or have I hit a bug?
It's not completely clear to me whether this is a bug or not, although enough people think it's counter-intuitive that it might be worth changing. The problem is that both answers are correct, depending on your interpretation of how author__name should be interpreted (which is a subtle issue, so it's easy for somebody to think the answer is obvious and should "obviously" be the way they see it, but it genuinely is a two-sided issue). The issue at work is that the Author class has no link to Article Two at all (since that article has no author). So, on the SQL level, it doesn't really make sense to query the authors table for things involving article two -- because article two is invisible to the authors; no linkage. However, it seems a lot of people would like author__name to be a way of transparently moving through the relationship as a formal matter, regardless of whether there's any actual data linkage or not and only then apply the filter to the field we end up in. I'm semi-convinced by this argument, so in the QuerySet rewrite I'm doing, I'm making the latter situation be the "correct" answer. By the way, this is ticket #2080 (marked as a dupe of #1801, but #2080 is probably the clearer explanation of the issue). Regards, Malcolm -- Despite the cost of living, have you noticed how popular it remains? http://www.pointy-stick.com/blog/ --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---