Consider the same model definition:
{{{
#!python
from django.db import models
class Rectangle(models.Model):
length = models.IntegerField(null=True)
width = models.IntegerField(null=True)
}}}
Then compare the behaviour of excluding when directly accessing the field
vs. using an alias or annotation:
{{{
#!python
In [5]: str(Rectangle.objects.exclude(length=123).values("pk").query)
Out[5]: 'SELECT "geometry_rectangle"."id" FROM "geometry_rectangle" WHERE
NOT ("geometry_rectangle"."length" = 123 AND "geometry_rectangle"."length"
IS NOT NULL)'
In [6]:
str(Rectangle.objects.alias(aliased_length=F("length")).exclude(aliased_length=123).values("pk").query)
Out[6]: 'SELECT "geometry_rectangle"."id" FROM "geometry_rectangle" WHERE
NOT ("geometry_rectangle"."length" = 123)'
In [7]:
str(Rectangle.objects.annotate(aliased_length=F("length")).exclude(aliased_length=123).values("pk").query)
Out[7]: 'SELECT "geometry_rectangle"."id" FROM "geometry_rectangle" WHERE
NOT ("geometry_rectangle"."length" = 123)'
}}}
The expected behaviour would be that when using an alias or annotation,
the {{{IS NOT NULL}}} condition would be added when using exclude.
--
Ticket URL: <https://code.djangoproject.com/ticket/32896>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* resolution: => duplicate
Comment:
Duplicate of #32398.
--
Ticket URL: <https://code.djangoproject.com/ticket/32896#comment:1>