jowilf / starlette-admin

Fast, beautiful and extensible administrative interface framework for Starlette & FastApi applications
https://jowilf.github.io/starlette-admin/
MIT License
618 stars 59 forks source link

Bug: ID pk gives JSON serialization error when excluded from the list #564

Open jhq223 opened 3 months ago

jhq223 commented 3 months ago

Describe the bug ID pk gives JSON serialization error when excluded from the list

  File "D:\Documents\Code\Python\starlette-admin\.venv\Lib\site-packages\starlette\responses.py", line 184, in __init__
    super().__init__(content, status_code, headers, media_type, background)
  File "D:\Documents\Code\Python\starlette-admin\.venv\Lib\site-packages\starlette\responses.py", line 41, in __init__
    self.body = self.render(content)
                ^^^^^^^^^^^^^^^^^^^^
  File "D:\Documents\Code\Python\starlette-admin\.venv\Lib\site-packages\starlette\responses.py", line 187, in render
    return json.dumps(
           ^^^^^^^^^^^
  File "D:\.ENV\Python\Lib\json\__init__.py", line 238, in dumps
    **kw).encode(obj)
          ^^^^^^^^^^^
  File "D:\.ENV\Python\Lib\json\encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\.ENV\Python\Lib\json\encoder.py", line 258, in iterencode
    return _iterencode(o, 0)
           ^^^^^^^^^^^^^^^^^
  File "D:\.ENV\Python\Lib\json\encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type ObjectId is not JSON serializable

To Reproduce Add id to exclude_fields_from_list

# Add views
class UserView(ModelView):
    exclude_fields_from_list = ["id"]

run uvicorn examples.mongoengine.app:app An exception will be thrown when the user data table has data.

Environment (please complete the following information):

Additional context This error is similar to that #552

jowilf commented 2 months ago

This error is similar to that https://github.com/jowilf/starlette-admin/issues/552

Yes, we need to implement get_serialized_pk_value for other backends as well