Skip to content

Commit

Permalink
perf: Set the default value for schema_read to schema_update(you can …
Browse files Browse the repository at this point in the history
…disable read_fields by setting them to None)
  • Loading branch information
amisadmin committed Jan 12, 2024
1 parent 7eb7ce0 commit 8c009d5
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
4 changes: 3 additions & 1 deletion fastapi_amis_admin/crud/_sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion fastapi_amis_admin/crud/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions tests/test_crud/test_SqlalchemyCrud_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 8c009d5

Please sign in to comment.