diff --git a/sqladmin/models.py b/sqladmin/models.py index 5215ee8e..059bed41 100644 --- a/sqladmin/models.py +++ b/sqladmin/models.py @@ -1200,12 +1200,16 @@ async def generate() -> AsyncGenerator[str, None]: len_data = len(data) last_idx = len_data - 1 separator = "," if len_data > 1 else "" + row_dict = {} for idx, row in enumerate(data): - row_dict = { - name: str(await self.get_prop_value(row, name)) - for name in self._export_prop_names - } + for name in self._export_prop_names: + value = await self.get_prop_value(row, name) + try: + json.dumps(value) + except TypeError: + value = str(value) + row_dict[name] = value yield json.dumps(row_dict) + (separator if idx < last_idx else "") yield "]" diff --git a/tests/test_views/test_view_sync.py b/tests/test_views/test_view_sync.py index 00fc8448..c55b142d 100644 --- a/tests/test_views/test_view_sync.py +++ b/tests/test_views/test_view_sync.py @@ -753,7 +753,7 @@ def test_export_json_complex_model(client: TestClient) -> None: response = client.get("/admin/address/export/json") assert response.text == json.dumps( - [{"id": "1", "user_id": "1", "user": "User 1", "user.profile.id": "None"}] + [{"id": 1, "user_id": 1, "user": "User 1", "user.profile.id": None}] )