Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional relationships fields in pydantic_model_creator #1356

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions tortoise/contrib/pydantic/creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down