more docs

This commit is contained in:
dni ⚡
2024-10-10 09:07:39 +02:00
parent e1fb82cea5
commit c0db3468af
+13 -3
View File
@@ -234,7 +234,9 @@ class Connection(Compat):
""", """,
parsed_values, parsed_values,
) )
count = int(result.get("count", 0)) row = result.mappings().first()
result.close()
count = int(row.get("count", 0))
else: else:
count = len(rows) count = len(rows)
else: else:
@@ -587,8 +589,10 @@ def update_query(
def model_to_dict(model: BaseModel) -> dict: def model_to_dict(model: BaseModel) -> dict:
""" """
Convert a Pydantic model to a dictionary with JSON-encoded nested models Convert a Pydantic model to a dictionary with JSON-encoded nested models
TODO: no recursion, maybe make them recursive? private fields starting with _ are ignored
:param model: Pydantic model
""" """
# TODO: no recursion, maybe make them recursive?
_dict = model.dict() _dict = model.dict()
for key, value in _dict.items(): for key, value in _dict.items():
if key.startswith("_"): if key.startswith("_"):
@@ -602,9 +606,15 @@ def model_to_dict(model: BaseModel) -> dict:
def dict_to_model(_dict: dict, model: type[TModel]) -> TModel: def dict_to_model(_dict: dict, model: type[TModel]) -> TModel:
""" """
Convert a dictionary with JSON-encoded nested models to a Pydantic model Convert a dictionary with JSON-encoded nested models to a Pydantic model
TODO: no recursion, maybe make them recursive? :param _dict: Dictionary from database
:param model: Pydantic model
""" """
# TODO: no recursion, maybe make them recursive?
# TODO: check why keys are sometimes not in the dict
for key, value in _dict.items(): for key, value in _dict.items():
if key not in model.__fields__:
# logger.warning(f"Converting {key} to model `{model}`.")
continue
type_ = model.__fields__[key].type_ type_ = model.__fields__[key].type_
if issubclass(type_, BaseModel): if issubclass(type_, BaseModel):
_dict[key] = type_.construct(**json.loads(value)) _dict[key] = type_.construct(**json.loads(value))