Commit 33abffb 1 parent 99a1425 commit 33abffb Copy full SHA for 33abffb
File tree 4 files changed +14
-8
lines changed
4 files changed +14
-8
lines changed Original file line number Diff line number Diff line change @@ -106,7 +106,13 @@ def convert_fields_list(
106
106
):
107
107
converted_fields .append (HasOne (attr .key , identity = identity ))
108
108
else :
109
- converted_fields .append (HasMany (attr .key , identity = identity ))
109
+ converted_fields .append (
110
+ HasMany (
111
+ attr .key ,
112
+ identity = identity ,
113
+ collection_class = attr .collection_class or list ,
114
+ )
115
+ )
110
116
elif isinstance (attr , ColumnProperty ):
111
117
assert (
112
118
len (attr .columns ) == 1
Original file line number Diff line number Diff line change 14
14
from sqlalchemy .sql import Select
15
15
from starlette .requests import Request
16
16
from starlette .responses import Response
17
- from starlette_admin import BaseField
17
+ from starlette_admin import BaseField , HasMany
18
18
from starlette_admin ._types import RequestAction
19
19
from starlette_admin .contrib .sqla .converters import (
20
20
BaseSQLAModelConverter ,
@@ -521,12 +521,10 @@ async def _arrange_data(
521
521
for field in self .get_fields_list (request , request .state .action ):
522
522
if isinstance (field , RelationField ) and data [field .name ] is not None :
523
523
foreign_model = self ._find_foreign_model (field .identity ) # type: ignore
524
- if not field .multiple :
525
- arranged_data [field .name ] = await foreign_model .find_by_pk (
526
- request , data [field .name ]
527
- )
524
+ if isinstance (field , HasMany ):
525
+ arranged_data [field .name ] = field .collection_class (await foreign_model .find_by_pks (request , data [field .name ])) # type: ignore[call-arg]
528
526
else :
529
- arranged_data [field .name ] = await foreign_model .find_by_pks (
527
+ arranged_data [field .name ] = await foreign_model .find_by_pk (
530
528
request , data [field .name ]
531
529
)
532
530
else :
Original file line number Diff line number Diff line change 9
9
from typing import (
10
10
Any ,
11
11
Callable ,
12
+ Collection ,
12
13
Dict ,
13
14
List ,
14
15
Optional ,
@@ -1094,6 +1095,7 @@ class HasMany(RelationField):
1094
1095
"""A field representing a "has-many" relationship between two models."""
1095
1096
1096
1097
multiple : bool = True
1098
+ collection_class : Union [Type [Collection [Any ]], Callable [[], Collection [Any ]]] = list
1097
1099
1098
1100
1099
1101
@dataclass (init = False )
Original file line number Diff line number Diff line change @@ -38,7 +38,7 @@ class User(Base, IDMixin):
38
38
39
39
name = Column (String (100 ))
40
40
41
- todos = relationship ("Todo" , back_populates = "user" )
41
+ todos = relationship ("Todo" , back_populates = "user" , collection_class = set )
42
42
43
43
44
44
class Todo (Base , IDMixin ):
You can’t perform that action at this time.
0 commit comments