From 8c009d59c1d4a35ee1a789628858d6fbb01499bd Mon Sep 17 00:00:00 2001 From: atomi <1456417373@qq.com> Date: Fri, 12 Jan 2024 20:36:36 +0800 Subject: [PATCH] perf: Set the default value for schema_read to schema_update(you can disable read_fields by setting them to None) --- fastapi_amis_admin/crud/_sqlalchemy.py | 4 +++- fastapi_amis_admin/crud/base.py | 2 +- tests/test_crud/test_SqlalchemyCrud_fields.py | 1 + 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/fastapi_amis_admin/crud/_sqlalchemy.py b/fastapi_amis_admin/crud/_sqlalchemy.py index 6af162d..55278a1 100644 --- a/fastapi_amis_admin/crud/_sqlalchemy.py +++ b/fastapi_amis_admin/crud/_sqlalchemy.py @@ -316,8 +316,10 @@ def _create_schema_filter(self) -> Type[SchemaFilterT]: ) def _create_schema_read(self) -> Optional[Type[SchemaReadT]]: - if not self.read_fields: + if self.read_fields is None: return None + # Set the read fields to the schema update if not provided + self.read_fields = self.read_fields or model_fields(self.schema_update) # Filter out any non-model fields from the read fields modelfields = self.parser.filter_modelfield(self.read_fields) # Create the schema using the model fields diff --git a/fastapi_amis_admin/crud/base.py b/fastapi_amis_admin/crud/base.py index 7d8ff1b..4db7409 100644 --- a/fastapi_amis_admin/crud/base.py +++ b/fastapi_amis_admin/crud/base.py @@ -81,8 +81,8 @@ def register_crud( self.schema_list = schema_list or self.schema_list or self._create_schema_list() self.schema_filter = schema_filter or self.schema_filter or self._create_schema_filter() self.schema_create = schema_create or self.schema_create or self._create_schema_create() - self.schema_read = schema_read or self.schema_read or self._create_schema_read() self.schema_update = schema_update or self.schema_update or self._create_schema_update() + self.schema_read = schema_read or self.schema_read or self._create_schema_read() self.list_per_page_max = list_per_page_max or self.list_per_page_max self.paginator = Paginator(perPageMax=self.list_per_page_max) self.router.add_api_route( diff --git a/tests/test_crud/test_SqlalchemyCrud_fields.py b/tests/test_crud/test_SqlalchemyCrud_fields.py index 27e0ef5..ea96393 100644 --- a/tests/test_crud/test_SqlalchemyCrud_fields.py +++ b/tests/test_crud/test_SqlalchemyCrud_fields.py @@ -364,6 +364,7 @@ class ArticleCrud(SqlalchemyCrud): async def test_read_fields_and_schema_read_is_none(app: FastAPI, models): class ArticleCrud(SqlalchemyCrud): router_prefix = "/article" + read_fields = None ins = ArticleCrud(models.Article, db.engine).register_crud()