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

Fix JSON export trailing comma #843

Merged
Merged
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
16 changes: 9 additions & 7 deletions sqladmin/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,16 +403,16 @@ def formatter(model, attribute):
Normally, objects have three save options:
``Save`, `Save and continue editing` and `Save and add another`.

If save_as is True, `Save and add another` will be replaced
by a `Save as new` button
that creates a new object (with a new ID)
If save_as is True, `Save and add another` will be replaced
by a `Save as new` button
that creates a new object (with a new ID)
rather than updating the existing object.

By default, `save_as` is set to `False`.
"""

save_as_continue: ClassVar[bool] = True
"""When `save_as=True`, the default redirect after saving the new object
"""When `save_as=True`, the default redirect after saving the new object
is to the edit view for that object.
If you set `save_as_continue=False`, the redirect will be to the list view.

Expand Down Expand Up @@ -1194,14 +1194,16 @@ async def _export_json(
) -> StreamingResponse:
async def generate() -> AsyncGenerator[str, None]:
yield "["
separator = "," if len(data) > 1 else ""
len_data = len(data)
last_idx = len_data - 1
separator = "," if len_data > 1 else ""

for row in data:
for idx, row in enumerate(data):
row_dict = {
name: str(await self.get_prop_value(row, name))
for name in self._export_prop_names
}
yield json.dumps(row_dict) + separator
yield json.dumps(row_dict) + (separator if idx < last_idx else "")

yield "]"

Expand Down
Loading