On Saturday, November 21, 2015, Carsten Fuchs <carsten.fu...@cafu.de> wrote:

> Hi Anssi,
>
> Am 2015-11-21 um 12:50 schrieb Anssi Kääriäinen:
>
>>     In summary, the imaginary query of comment 14
>>
>>          Blog.objects.filter(entry__tag__name='django',
>>     entry__author_count__ne=2)
>>
>>
>> This isn't a real query. There isn't a field author_count, the query
>> needs an annotation somewhere. So, I don't think this argument is
>> convincing without the annotation (note that the place of the annotation
>> matters). In addition, providing example data and the expected results
>> would be great, too.
>>
>
> Well, yes, this is a fictional example, but if you replace author_count by
> some valid field that doesn't require an annotation, e.g. author__name, the
> (imaginary) query should be valid (for the purpose of demonstrating the
> problem)?


The author_count name suggested this was an aggregation. If this is just a
regular field, then things are a bit simpler. Note that negated Q-object +
filter and exclude() queries are the same thing.

>
> The key issue is really this, quoted from the linked Django documentation:
>
> "To handle both of these situations, Django has a consistent way of
>> processing
>> filter() calls. 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."
>>
>
> That is, sometimes we *have* to put several filters into a single filter()
> call to obtain the desired set. If such a situation requires a negation,
> exclude() cannot help, because "[...], they apply to *any* object linked to
> the primary model, not necessarily those objects that were selected by an
> earlier filter() call".
>
>
There is a fix for exactly this issue in pr
https://github.com/django/django/pull/4385. After the pr, you could just
use .filter(Q(entry__tags__name='django')&~Q(entry__author_count=2)).

The discussion seems to miss a real definition of what exactly the ne
>> lookup should do. There are two ways to implement ne, one is as a
>> complement of exact, another is as the != operator. In SQL the first one
>> is "col != val OR col IS NULL", the latter one is just "col != val".
>>
>
> Thanks for pointing this out, I wasn't aware of this (in this context)
> before. It seems to be another facet in the overall problem, but
> independent from the above, isn't it? (In my normal, "non-negated" queries,
> where required I account for NULLs explicitly all the time...)


Yeah. We should correct negated filtering to work as documented when
combined with other conditions.

So, to fix the issue in comment 14 of the ticket, the above mentioned PR is
the right fix. If you want the SQL != operator, then we need a new lookup.

 - Anssi


> Best regards,
> Carsten
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django developers  (Contributions to Django itself)" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-developers+unsubscr...@googlegroups.com.
> To post to this group, send email to django-developers@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-developers.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-developers/56506174.4050609%40cafu.de
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/CALMtK1G3N-8zcXajSFnwuc3_THi1v5h%3DHxbivbGEV7dN_zxVjw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to