Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save values types as is if possible while JSON export #865

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions sqladmin/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "]"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_views/test_view_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}]
)


Expand Down
Loading