Skip to content

Commit a06af1b

Browse files
committed
fix(MongoModel) : convert_list avoid None and refacto
1 parent c6dc5b6 commit a06af1b

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

fastapi_crudrouter_mongodb/core/models/mongo_model.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,19 @@ def convert_to(self, model: BaseModel):
6565
return model(**new_model)
6666

6767
def _convert_list(self, list_to_convert: list):
68-
if isinstance(list_to_convert[0], dict):
69-
new_list = []
70-
for sub_object in list_to_convert:
71-
for sub_field in sub_object:
72-
sub_value = sub_object[sub_field]
68+
if not isinstance(list_to_convert[0], dict):
69+
return list_to_convert
70+
new_list = []
71+
for sub_object in list_to_convert:
72+
for sub_field in sub_object:
73+
sub_value = sub_object[sub_field]
74+
if sub_value is not None:
7375
sub_object[sub_field] = (
74-
sub_value if type(sub_value) is not ObjectId else str(sub_value)
76+
sub_value if type(sub_value) is not ObjectId else str(sub_value)
7577
)
76-
new_list.append(sub_object)
77-
return new_list
78-
return list_to_convert
78+
new_list.append(sub_object)
79+
return new_list
80+
7981

8082
def __init__(self, **pydict):
8183
super().__init__(**pydict)

0 commit comments

Comments
 (0)