summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2022-04-19 08:24:24 +0200
committerGitHub <noreply@github.com>2022-04-19 08:24:24 +0200
commitf4f2afeb457429f55d6325ed557f8e92a74ee028 (patch)
tree266fcd7150fa098feddffcb85a53eaf9431fbf41
parent903702dfb1461cbb4ab93f841651e6882a72d337 (diff)
Refs #32226 -- Fixed JSON format of QuerySet.explain() on PostgreSQL when format is uppercased.
Follow up to aba9c2de669dcc0278c7ffde7981be91801be00b.
-rw-r--r--django/db/models/sql/compiler.py5
-rw-r--r--tests/queries/test_explain.py6
2 files changed, 6 insertions, 5 deletions
diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
index 1285b647d8..13b606255c 100644
--- a/django/db/models/sql/compiler.py
+++ b/django/db/models/sql/compiler.py
@@ -1434,9 +1434,8 @@ class SQLCompiler:
result = list(self.execute_sql())
# Some backends return 1 item tuples with strings, and others return
# tuples with integers and strings. Flatten them out into strings.
- output_formatter = (
- json.dumps if self.query.explain_info.format == "json" else str
- )
+ format_ = self.query.explain_info.format
+ output_formatter = json.dumps if format_ and format_.lower() == "json" else str
for row in result[0]:
if not isinstance(row, str):
yield " ".join(output_formatter(c) for c in row)
diff --git a/tests/queries/test_explain.py b/tests/queries/test_explain.py
index 43ff0bdef3..7932eb14f2 100644
--- a/tests/queries/test_explain.py
+++ b/tests/queries/test_explain.py
@@ -41,14 +41,16 @@ class ExplainTests(TestCase):
)
self.assertIsInstance(result, str)
self.assertTrue(result)
- if format == "xml":
+ if not format:
+ continue
+ if format.lower() == "xml":
try:
xml.etree.ElementTree.fromstring(result)
except xml.etree.ElementTree.ParseError as e:
self.fail(
f"QuerySet.explain() result is not valid XML: {e}"
)
- elif format == "json":
+ elif format.lower() == "json":
try:
json.loads(result)
except json.JSONDecodeError as e: