From e2f84cc97048f3e851ea2985f89a34f8242968b0 Mon Sep 17 00:00:00 2001 From: Juergen Brendel Date: Thu, 24 Oct 2024 22:11:25 +1300 Subject: [PATCH] Fix JSON export trailing comma (#843) --- sqladmin/models.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/sqladmin/models.py b/sqladmin/models.py index c143a897..98991372 100644 --- a/sqladmin/models.py +++ b/sqladmin/models.py @@ -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. @@ -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 "]"