From 3f96cf7c58b76d119c644506339fb09a4c89c5d2 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Tue, 8 Mar 2022 11:56:29 -0800 Subject: [PATCH] Fix error if array contains datetimes, closes #146 --- django_sql_dashboard/utils.py | 2 +- test_project/test_dashboard.py | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/django_sql_dashboard/utils.py b/django_sql_dashboard/utils.py index dc9568c..bdd197f 100644 --- a/django_sql_dashboard/utils.py +++ b/django_sql_dashboard/utils.py @@ -52,7 +52,7 @@ def displayable_rows(rows): fixed_row = [] for cell in row: if isinstance(cell, (dict, list)): - cell = json.dumps(cell) + cell = json.dumps(cell, default=str) fixed_row.append(cell) fixed.append(fixed_row) return fixed diff --git a/test_project/test_dashboard.py b/test_project/test_dashboard.py index 7f5110e..4fbdd8f 100644 --- a/test_project/test_dashboard.py +++ b/test_project/test_dashboard.py @@ -127,7 +127,15 @@ def test_dashboard_sql_errors(admin_client, sql, expected_error): @pytest.mark.parametrize( "sql,expected_columns,expected_rows", - (("select 'abc' as one, 'bcd' as one", ["one", "one"], [["abc", "bcd"]]),), + ( + ("select 'abc' as one, 'bcd' as one", ["one", "one"], [["abc", "bcd"]]), + ("select ARRAY[1, 2, 3]", ["array"], [["[\n 1,\n 2,\n 3\n]"]]), + ( + "select ARRAY[TIMESTAMP WITH TIME ZONE '2004-10-19 10:23:54+02']", + ["array"], + [['[\n "2004-10-19 08:23:54+00:00"\n]']], + ), + ), ) def test_dashboard_sql_queries(admin_client, sql, expected_columns, expected_rows): response = admin_client.post("/dashboard/", {"sql": sql}, follow=True)