print(var.query)
# select * from modelname where not ( field1 != '' and field1 is not null
and field1 is null ) which is always a empty set.
Expected Query:
# select * from modelname where not ( field1 != '' and field1 is null )
OR
# select * from modelname where not ((field1 != '' and field1 is not null)
and field1 is null )
which gives resultant set with non empty and not null records.
--
Ticket URL: <https://code.djangoproject.com/ticket/32476>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* Attachment "Buggy.png" added.
ORM Query and SQL generated Query
* status: new => closed
* resolution: => invalid
Comment:
That's not an issue in the ORM but in your filters, you described the set
of rows that is always empty: `field1 IS NULL` and `field1 = ''` (which
implies that `field1 IS NOT NULL`). Also, wrapping conditions with the
same logical operator doesn't change anything, because
{{{
NOT (field1 != '' AND field1 IS NOT NULL and field1 IS NULL)
}}}
is equivalent to
{{{
NOT ((field1 != '' AND field1 IS NOT NULL) and field1 IS NULL)
}}}
You should use `.exclude(Q(field1__isnull=True) | Q(field1=''))`.
Please don't use Trac as a support channel in the future. Closing per
TicketClosingReasons/UseSupportChannels.
--
Ticket URL: <https://code.djangoproject.com/ticket/32476#comment:1>