From e5e366b4abee72a390b148e5837126639d88f816 Mon Sep 17 00:00:00 2001 From: Vasily <89313005745@mail.ru> Date: Wed, 23 Oct 2024 14:57:28 +0300 Subject: [PATCH] Fix export json related model (#837) --- sqladmin/models.py | 2 +- tests/test_views/test_view_sync.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/sqladmin/models.py b/sqladmin/models.py index c9fadbe0..c143a897 100644 --- a/sqladmin/models.py +++ b/sqladmin/models.py @@ -1198,7 +1198,7 @@ async def generate() -> AsyncGenerator[str, None]: for row in data: row_dict = { - name: await self.get_prop_value(row, name) + name: str(await self.get_prop_value(row, name)) for name in self._export_prop_names } yield json.dumps(row_dict) + separator diff --git a/tests/test_views/test_view_sync.py b/tests/test_views/test_view_sync.py index a9904ab0..00fc8448 100644 --- a/tests/test_views/test_view_sync.py +++ b/tests/test_views/test_view_sync.py @@ -1,4 +1,5 @@ import enum +import json from typing import Any, Generator import pytest @@ -741,6 +742,21 @@ def test_export_json(client: TestClient) -> None: assert response.text == '[{"name": "Daniel", "status": "ACTIVE"}]' +def test_export_json_complex_model(client: TestClient) -> None: + with session_maker() as session: + user = User(name="Daniel", status="ACTIVE") + session.add(user) + session.commit() + address = Address(user_id=user.id) + session.add(address) + session.commit() + + response = client.get("/admin/address/export/json") + assert response.text == json.dumps( + [{"id": "1", "user_id": "1", "user": "User 1", "user.profile.id": "None"}] + ) + + def test_export_csv_row_count(client: TestClient) -> None: def row_count(resp) -> int: return resp.text.count("\r\n") - 1