Opened 8 years ago
Last modified 8 years ago
#29166 closed Bug
in lookup doesn't work with lists in a When clause — at Version 1
| Reported by: | Matthew Pava | Owned by: | nobody |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 2.0 |
| Severity: | Release blocker | Keywords: | lookup in |
| Cc: | Дилян Палаузов | Triage Stage: | Ready for checkin |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
I have an annotation that worked fine in Django 1.11, but I just discovered that it doesn't work with Django 2.0. The error message reported:
TypeError: unhashable type: 'list'
I have an Order model that has a status and status_date field, and here's the query that worked in the past (simplified):
Order.objects.annotate(
end_date=Case(
When(
status__in=[3, 2],
then=Cast(F('status_date'), DateField())
),
default=Value(timezone.now().date())
)
)
Note the list used as the arg in the __in lookup.
My current workaround is to use Q objects with the | operator.
Order.objects.annotate(
end_date=Case(
When(
Q(status=3) | Q(status=2),
then=Cast(F('status_date'), DateField())
),
default=Value(timezone.now().date())
)
)
Others may not find that workaround as practical.
I have also not verified this issue in other Expressions in the ORM.