diff --git a/tortoise/contrib/pydantic/creator.py b/tortoise/contrib/pydantic/creator.py index ff1453b19..d99509909 100644 --- a/tortoise/contrib/pydantic/creator.py +++ b/tortoise/contrib/pydantic/creator.py @@ -376,7 +376,7 @@ def get_submodel(_model: "Type[Model]") -> Optional[Type[PydanticModel]]: if model: if fdesc.get("nullable"): fconfig["nullable"] = True - if fdesc.get("nullable") or field_default is not None: + if fdesc.get("nullable") or field_default is not None or fname in optional: model = Optional[model] # type: ignore pannotations[fname] = model @@ -388,7 +388,12 @@ def get_submodel(_model: "Type[Model]") -> Optional[Type[PydanticModel]]: ): model = get_submodel(fdesc["python_type"]) if model: - pannotations[fname] = List[model] # type: ignore + if fdesc.get("nullable") or field_default is not None or fname in optional: + model = Optional[List[model]] # type: ignore + else: + model = List[model] # type: ignore + + pannotations[fname] = model # type: ignore # Computed fields as methods elif field_type is callable: