[Django] #29166: in lookup doesn't work with lists in a When clause

7 views
Skip to first unread message

Django

unread,
Feb 27, 2018, 11:15:12 AM2/27/18
to django-...@googlegroups.com
#29166: in lookup doesn't work with lists in a When clause
-------------------------------------+-------------------------------------
Reporter: Matthew | Owner: nobody
Pava |
Type: Bug | Status: new
Component: Database | Version: 2.0
layer (models, ORM) |
Severity: Normal | Keywords: lookup in
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/29166>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 27, 2018, 11:15:50 AM2/27/18
to django-...@googlegroups.com
#29166: in lookup doesn't work with lists in a When clause
-------------------------------------+-------------------------------------
Reporter: Matthew Pava | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:

Keywords: lookup in | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Matthew Pava:

Old description:

New description:

I have also not verified this issue in other `Expression`s in the ORM.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/29166#comment:1>

Django

unread,
Feb 27, 2018, 12:32:01 PM2/27/18
to django-...@googlegroups.com
#29166: in lookup doesn't work with lists in a When clause
-------------------------------------+-------------------------------------
Reporter: Matthew Pava | Owner: felixxm
Type: Bug | Status: assigned

Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: lookup in | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

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

* status: new => assigned
* owner: nobody => felixxm
* severity: Normal => Release blocker
* stage: Unreviewed => Accepted


Comment:

Regression in 19b2dfd1bfe7fd716dd3d8bfa5f972070d83b42f.

--
Ticket URL: <https://code.djangoproject.com/ticket/29166#comment:2>

Django

unread,
Feb 27, 2018, 2:56:01 PM2/27/18
to django-...@googlegroups.com
#29166: in lookup doesn't work with lists in a When clause
-------------------------------------+-------------------------------------
Reporter: Matthew Pava | Owner: felixxm
Type: Bug | Status: assigned
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: lookup in | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

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

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/9734 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/29166#comment:3>

Django

unread,
Feb 28, 2018, 5:08:27 AM2/28/18
to django-...@googlegroups.com
#29166: in lookup doesn't work with lists in a When clause
-------------------------------------+-------------------------------------
Reporter: Matthew Pava | Owner: felixxm
Type: Bug | Status: assigned
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: lookup in | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

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

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/29166#comment:4>

Django

unread,
Feb 28, 2018, 12:05:54 PM2/28/18
to django-...@googlegroups.com
#29166: in lookup doesn't work with lists in a When clause
-------------------------------------+-------------------------------------
Reporter: Matthew Pava | Owner: felixxm
Type: Bug | Status: closed

Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Release blocker | Resolution: fixed

Keywords: lookup in | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by GitHub <noreply@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"54f80430be4a9adf1fc00b4ca17547415fafc69b" 54f8043]:
{{{
#!CommitTicketReference repository=""
revision="54f80430be4a9adf1fc00b4ca17547415fafc69b"
Fixed #29166 -- Fixed crash in When() expression with a list argument.

Thanks Matthew Pava for the report and Tim Graham and Carlton Gibson for
reviews.
Regression in 19b2dfd1bfe7fd716dd3d8bfa5f972070d83b42f.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29166#comment:5>

Django

unread,
Feb 28, 2018, 12:16:37 PM2/28/18
to django-...@googlegroups.com
#29166: in lookup doesn't work with lists in a When clause
-------------------------------------+-------------------------------------
Reporter: Matthew Pava | Owner: felixxm
Type: Bug | Status: closed
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: lookup in | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"10a20e4b37da94fd55b4e9a13e634c2a379bd9e0" 10a20e4]:
{{{
#!CommitTicketReference repository=""
revision="10a20e4b37da94fd55b4e9a13e634c2a379bd9e0"
[2.0.x] Fixed #29166 -- Fixed crash in When() expression with a list
argument.

Thanks Matthew Pava for the report and Tim Graham and Carlton Gibson for
reviews.
Regression in 19b2dfd1bfe7fd716dd3d8bfa5f972070d83b42f.

Backport of 54f80430be4a9adf1fc00b4ca17547415fafc69b from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29166#comment:6>

Django

unread,
Mar 2, 2018, 10:22:56 AM3/2/18
to django-...@googlegroups.com
#29166: in lookup doesn't work with lists in a When clause
-------------------------------------+-------------------------------------
Reporter: Matthew Pava | Owner: felixxm
Type: Bug | Status: closed
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: lookup in | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Дилян Палаузов):

{{{
diff --git a/django/db/models/expressions.py
b/django/db/models/expressions.py
+ return hash((path, output_field) + args + tuple([
+ (key, tuple(value)) if isinstance(value, list) else (key,
value)
+ for key, value in kwargs.items()
+ ]))
}}}

Aren't here the {{{[}}} and {{{]}}} superflous, as the parameter of both
{{{tuple()}}} and {{{[]}}} have to be iterable, and this the type of the
inlined {{{for}}}:
{{{
>>> tuple([(x,y) for x, y in {'a': 1, 'b':2, 'c':3}.items()])
(('a', 1), ('b', 2), ('c', 3))
>>> tuple((x,y) for x, y in {'a': 1, 'b':2, 'c':3}.items())
(('a', 1), ('b', 2), ('c', 3))
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29166#comment:7>

Django

unread,
Mar 2, 2018, 10:24:32 AM3/2/18
to django-...@googlegroups.com
#29166: in lookup doesn't work with lists in a When clause
-------------------------------------+-------------------------------------
Reporter: Matthew Pava | Owner: felixxm
Type: Bug | Status: closed
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: lookup in | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Дилян Палаузов):

* cc: Дилян Палаузов (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/29166#comment:8>

Reply all
Reply to author
Forward
0 new messages