Skip to content

Commit 1dc43af

Browse files
authored
Merge pull request #32 from benavlabs/read-schema
select_schema functional
2 parents 5a84632 + 76d0acf commit 1dc43af

File tree

5 files changed

+723
-5
lines changed

5 files changed

+723
-5
lines changed

crudadmin/admin_interface/crud_admin.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,7 @@ def add_view(
803803
update_schema: Type[BaseModel],
804804
update_internal_schema: Optional[Type[BaseModel]] = None,
805805
delete_schema: Optional[Type[BaseModel]] = None,
806+
select_schema: Optional[Type[BaseModel]] = None,
806807
include_in_models: bool = True,
807808
allowed_actions: Optional[set[str]] = None,
808809
password_transformer: Optional[Any] = None,
@@ -819,6 +820,7 @@ def add_view(
819820
update_schema: Pydantic schema for update operations
820821
update_internal_schema: Internal schema for special update cases
821822
delete_schema: Schema for delete operations
823+
select_schema: Optional schema for read operations (excludes fields from queries)
822824
include_in_models: Show in models list in admin UI
823825
allowed_actions: **Set of allowed operations:**
824826
- **"view"**: Allow viewing records
@@ -835,6 +837,7 @@ def add_view(
835837
Notes:
836838
- Forms are auto-generated with field types determined from Pydantic schemas
837839
- Actions controlled by allowed_actions parameter
840+
- Use select_schema to exclude problematic fields (e.g., TSVector) from read operations
838841
- Use password_transformer for models with password fields that need hashing
839842
840843
URL Routes:
@@ -872,6 +875,29 @@ class UserUpdate(BaseModel):
872875
)
873876
```
874877
878+
Excluding problematic fields (e.g., TSVector):
879+
```python
880+
class DocumentCreate(BaseModel):
881+
title: str
882+
content: str
883+
# TSVector field excluded from this schema
884+
885+
class DocumentSelect(BaseModel):
886+
id: int
887+
title: str
888+
content: str
889+
created_at: datetime
890+
# search_vector (TSVector) field excluded
891+
892+
admin.add_view(
893+
model=Document,
894+
create_schema=DocumentCreate,
895+
update_schema=DocumentCreate,
896+
select_schema=DocumentSelect, # TSVector field excluded from reads
897+
allowed_actions={"view", "create", "update"}
898+
)
899+
```
900+
875901
User with password handling:
876902
```python
877903
from crudadmin.admin_interface.model_view import PasswordTransformer
@@ -1028,6 +1054,7 @@ class Config:
10281054
update_schema=update_schema,
10291055
update_internal_schema=update_internal_schema,
10301056
delete_schema=delete_schema,
1057+
select_schema=select_schema,
10311058
admin_site=self.admin_site,
10321059
allowed_actions=allowed_actions,
10331060
event_integration=self.event_integration,

crudadmin/admin_interface/model_view.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,10 @@ async def bulk_delete_endpoint_inner(
772772
f"{pk_name}__in": valid_ids
773773
}
774774
records_to_delete = await self.crud.get_multi(
775-
db=db, limit=len(valid_ids), **cast(Any, filter_criteria)
775+
db=db,
776+
limit=len(valid_ids),
777+
schema_to_select=self.select_schema,
778+
**cast(Any, filter_criteria),
776779
)
777780

778781
request.state.deleted_records = records_to_delete.get("data", [])
@@ -804,6 +807,7 @@ async def bulk_delete_endpoint_inner(
804807
db=db,
805808
offset=(adjusted_page - 1) * rows_per_page,
806809
limit=rows_per_page,
810+
schema_to_select=self.select_schema,
807811
)
808812

809813
items: Dict[str, Any] = {
@@ -947,6 +951,7 @@ async def get_model_admin_page_inner(
947951
limit=rows_per_page,
948952
sort_columns=sort_columns,
949953
sort_orders=sort_orders,
954+
schema_to_select=self.select_schema,
950955
**cast(Any, filter_criteria),
951956
)
952957

@@ -1053,7 +1058,9 @@ async def get_model_update_page_inner(
10531058
db: AsyncSession = Depends(self.session),
10541059
) -> Response:
10551060
"""Show a form to update an existing record by `id`."""
1056-
item = await self.crud.get(db=db, id=id)
1061+
item = await self.crud.get(
1062+
db=db, id=id, schema_to_select=self.select_schema
1063+
)
10571064
if not item:
10581065
return JSONResponse(
10591066
status_code=404, content={"message": f"Item with id {id} not found"}
@@ -1112,7 +1119,9 @@ async def form_update_endpoint_inner(
11121119
status_code=422, content={"message": "No id parameter provided"}
11131120
)
11141121

1115-
item = await self.crud.get(db=db, id=id)
1122+
item = await self.crud.get(
1123+
db=db, id=id, schema_to_select=self.select_schema
1124+
)
11161125
if not item:
11171126
return JSONResponse(
11181127
status_code=404, content={"message": f"Item with id {id} not found"}
@@ -1283,7 +1292,11 @@ async def table_body_content_inner(
12831292
filter_criteria[f"{search_column}__ilike"] = f"%{search_value}%"
12841293

12851294
items_result = await self.crud.get_multi(
1286-
db=db, offset=offset, limit=limit, **cast(Any, filter_criteria)
1295+
db=db,
1296+
offset=offset,
1297+
limit=limit,
1298+
schema_to_select=self.select_schema,
1299+
**cast(Any, filter_criteria),
12871300
)
12881301

12891302
items: Dict[str, Any] = {

0 commit comments

Comments
 (0)