#28214: IndexError for JSONField queries containing percent character in key
--------------------------------------------+------------------------
Reporter: mrsanders | Owner: (none)
Type: Bug | Status: new
Component: contrib.postgres | Version: 1.11
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------------+------------------------
Using Django 1.11.1 and Postgres 9.5.6.
A JSON field may legitimately contain a percent symbol in a key, for
example:
{{{#!javascript
{"%total": 10}
}}}
This may be queried as follows (assuming the JSONField in the Django model
is called 'my_json_field'):
{{{
>>> qs=MyModel.objects.filter(**{"my_json_field__%total": 10})
}}}
However when the Queryset is evaluated, it raises an IndexError:
{{{
>>> len(qs)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py",
line 232, in __len__
self._fetch_all()
File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py",
line 1103, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py",
line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
File "/usr/local/lib/python3.6/site-
packages/django/db/models/sql/compiler.py", line 886, in execute_sql
raise original_exception
File "/usr/local/lib/python3.6/site-
packages/django/db/models/sql/compiler.py", line 876, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python3.6/site-
packages/django/db/backends/utils.py", line 80, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/local/lib/python3.6/site-
packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
IndexError: tuple index out of range
}}}
...and when attempting to print the generated query, a ValueError is
raised:
{{{
>>> print(qs.query)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python3.6/site-
packages/django/db/models/sql/query.py", line 234, in __str__
return sql % params
ValueError: unsupported format character 't' (0x74) at index 1498
>>>
}}}
It seems that something isn't escaping the percent before doing a string
formatting operation.
Changing the query to double-up the percent symbol causes the query to
work (although it shouldn't):
{{{
>>> qs=MyModel.objects.filter(**{"my_json_field__%%total": 10}
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28214>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/052.4426e10e789a6814943f275b548cbfdf%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.