starlette-admin
starlette-admin copied to clipboard
Bug: ID pk gives JSON serialization error when excluded from the list
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):
- starlette-admin>=0.14.1
- uvicorn>=0.30.3
- mongoengine>=0.28.2
- ORM/ODMs: MongoEngine
- python: 3.12.4
Additional context This error is similar to that #552
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