{{{
qs = company.employee_set.annotate(past=Coalesce(LessThan(F('end_date'),
Value(date.today())), Value(False)))
qs = qs.order_by(...)
return list(qs.values_list(...))
}}}
The idea is to compare "end_date" (a DateField which can be null) with the
given Python datetime.date, i.e. in Python terms, something like this:
{{{
past = end_date < today if end_date is not None else False
}}}
This works as I expected on Django 4.0.4, but fails on 3.2.13 where is
causes the **can't adapt type 'LessThan'**. I tried a couple of other
formulations of the query, including moving the **Coalesce** to "inside":
{{{
...annotate(past=LessThan(Coalesce(F('end_date'), Value('1999-01-01'),
output_field=DateField()), Value(date.today())))
}}}
This also worked on Django 4.0.4, but failed on 3.2.13 with TypeError
"QuerySet.annotate() received non-expression(s):
<django.db.models.lookups.LessThan object at ...>."
Since a Lookup is a query expression, I assume this is a bug? Is there a
workaround I might try?
--
Ticket URL: <https://code.djangoproject.com/ticket/33635>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* resolution: => invalid
Comment:
Django 3.2 is in extended support and doesn't receive bugfixes (except
security patches) anymore. You can use `Case()` on Django 3.2:
{{{
.annotate(past=Case(
When(Q(end_date__lt=date.today()), then=True),
default=False,
output_field=BooleanField(),
))
}}}
or `ExpressionWrapper()`:
{{{
.annotate(past=ExpressionWrapper(
Q(end_date__lt=date.today()),
output_field=BooleanField(),
))
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/33635#comment:1>