Skip to content

Commit a7935fe

Browse files
laymonagefelixxm
authored andcommitted
[3.1.x] Fixed #32203 -- Fixed QuerySet.values()/values_list() crash on key transforms with non-string values on SQLite.
Thanks Gordon Wrigley for the report. Backport of fe6e582 from master
1 parent a2abeb3 commit a7935fe

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

django/db/models/fields/json.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ def deconstruct(self):
7575
def from_db_value(self, value, expression, connection):
7676
if value is None:
7777
return value
78+
# Some backends (SQLite at least) extract non-string values in their
79+
# SQL datatypes.
80+
if isinstance(expression, KeyTransform) and not isinstance(value, str):
81+
return value
7882
try:
7983
return json.loads(value, cls=self.decoder)
8084
except json.JSONDecodeError:

docs/releases/3.1.4.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ Bugfixes
2828
* Fixed a regression in Django 3.1 that caused suppressing connection errors
2929
when :class:`~django.db.models.JSONField` is used on SQLite
3030
(:ticket:`32224`).
31+
32+
* Fixed a crash on SQLite, when ``QuerySet.values()/values_list()`` contained
33+
key transforms for :class:`~django.db.models.JSONField` returning non-string
34+
primitive values (:ticket:`32203`).

tests/model_fields/test_jsonfield.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ def setUpTestData(cls):
262262
'k': {'l': 'm'},
263263
'n': [None],
264264
'o': '"quoted"',
265+
'p': 4.2,
265266
},
266267
[1, [2]],
267268
{'k': True, 'l': False},
@@ -684,10 +685,14 @@ def test_key_values(self):
684685
qs = NullableJSONModel.objects.filter(value__h=True)
685686
tests = [
686687
('value__a', 'b'),
688+
('value__c', 14),
687689
('value__d', ['e', {'f': 'g'}]),
690+
('value__h', True),
691+
('value__i', False),
688692
('value__j', None),
689693
('value__k', {'l': 'm'}),
690694
('value__n', [None]),
695+
('value__p', 4.2),
691696
]
692697
for lookup, expected in tests:
693698
with self.subTest(lookup=lookup):

0 commit comments

Comments
 (0)