[Django] #33635: Django 3.2.13 raises ProgrammingError "can't adapt type 'LessThan'"

260 views
Skip to first unread message

Django

unread,
Apr 11, 2022, 6:20:16 AM4/11/22
to django-...@googlegroups.com
#33635: Django 3.2.13 raises ProgrammingError "can't adapt type 'LessThan'"
-------------------------------------+-------------------------------------
Reporter: Shaheed | Owner: nobody
Haque |
Type: Bug | Status: new
Component: Database | Version: 3.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I'm using Postgres as my db engine. I have a couple of lines of code like
this:

{{{
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.

Django

unread,
Apr 11, 2022, 6:56:45 AM4/11/22
to django-...@googlegroups.com
#33635: Django 3.2.13 raises ProgrammingError "can't adapt type 'LessThan'"
-------------------------------------+-------------------------------------
Reporter: Shaheed Haque | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* 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>

Reply all
Reply to author
Forward
0 new messages