Skip to content

Commit 6a642cf

Browse files
committed
feat(MongoModel) : add convert dict
1 parent a06af1b commit 6a642cf

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

fastapi_crudrouter_mongodb/core/models/mongo_model.py

+8
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ def convert_to(self, model: BaseModel):
6060
if value is not None:
6161
if isinstance(value, list):
6262
value = self._convert_list(value)
63+
if isinstance(value, dict):
64+
value = self._convert_dict(value)
6365
new_model[field] = value if type(value) is not ObjectId else str(value)
6466

6567
return model(**new_model)
@@ -78,6 +80,12 @@ def _convert_list(self, list_to_convert: list):
7880
new_list.append(sub_object)
7981
return new_list
8082

83+
def _convert_dict(self, dict_to_convert: dict):
84+
return {
85+
field: value if type(value) is not ObjectId else str(value)
86+
for field, value in dict_to_convert.items()
87+
if (value is not None)
88+
}
8189

8290
def __init__(self, **pydict):
8391
super().__init__(**pydict)

0 commit comments

Comments
 (0)