id summary reporter owner description type status component version severity resolution keywords cc stage has_patch needs_docs needs_tests needs_better_patch easy ui_ux 7047 filter() with Q spanning ManyToMany relations return wrong results Waldemar Kornewald nobody "I'll try to simplify our situation, so the code samples have nothing to do with our actual models. Currently, we use sqlite with Python 2.5 and Django queryset-refactor (trunk has similar problems, though). We have models similar to these: {{{ #!python class Tag(Model): name = CharField(max_length=100) hits = ManyToManyField(Article, related_name='tags') class Category(Model): name = CharField(max_length=100) hits = ManyToManyField(Article, related_name='categories') }}} In our DB we have an Article object that has two tags ('taga' and 'tagb') and two categories ('cata' and 'catb'). Then we run the following query (find an Article that has both 'taga' and 'tagb' in the tags and/or categories): {{{ #!python Article.objects.filter( Q(categories__name__startswith='taga') \ | \ Q(tags__name__startswith='taga') \ , Q(categories__name__startswith='tagb') \ | \ Q(tags__name__startswith='tagb') \ ).distinct() }}} The query will not find the article (although it should). Now, if we swap the attribute accessing order (i.e., replace `Q(categories__...) | Q(tags__...)` with `Q(tags__...) | Q(categories__...)`) it'll work correctly. In other words: Only the first attribute (`categories__...`) will allow for matching multiple times whereas the second attribute (`tags__...`) only allows for matching once. Note, this actually seems to only depend on the attribute order of the *last* Q group (in the snippet it's the Q pair for 'tagb'). Other example queries that work correctly with the above code snippet: * 'cata' and 'catb' * 'taga' and 'cata' * 'cata' and 'taga' and 'catb'" closed Database layer (models, ORM) queryset-refactor fixed Accepted 0 0 0 0 0 0