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

use construct in from_row #887

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions ormar/models/model_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,12 @@ def from_row( # noqa: CFQ002

instance: Optional["Model"] = None
if item.get(cls.Meta.pkname, None) is not None:
item["__excluded__"] = cls.get_names_to_exclude(
excluded_fields = cls.get_names_to_exclude(
excludable=excludable, alias=table_prefix
)
instance = cast("Model", cls(**item))
instance = cast(
"Model", cls.construct(**item, _excluded_fields=excluded_fields)
)
instance.set_save_status(True)
return instance

Expand Down
13 changes: 9 additions & 4 deletions ormar/models/newbasemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,18 +825,23 @@ def json( # type: ignore # noqa A003

@classmethod
def construct(
cls: Type["T"], _fields_set: Optional["SetStr"] = None, **values: Any
cls: Type["T"],
_fields_set: Optional["SetStr"] = None,
_excluded_fields: Optional["SetStr"] = None,
**values: Any,
) -> "T":
for field_to_nullify in _excluded_fields or []:
values[field_to_nullify] = None
own_values = {
k: v for k, v in values.items() if k not in cls.extract_related_names()
}
model = cls.__new__(cls)
fields_values: Dict[str, Any] = {}
for name, field in cls.__fields__.items():
for name, field_to_nullify in cls.__fields__.items():
if name in own_values:
fields_values[name] = own_values[name]
elif not field.required:
fields_values[name] = field.get_default()
elif not field_to_nullify.required:
fields_values[name] = field_to_nullify.get_default()
fields_values.update(own_values)
object.__setattr__(model, "__dict__", fields_values)
model._initialize_internal_attributes()
Expand Down
6 changes: 6 additions & 0 deletions tests/test_model_definition/test_pydantic_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ def __init__(self, **kwargs):
kwargs["pydantic_test"] = PydanticTest(aa="random", bb=42)
super().__init__(**kwargs)

@classmethod
def construct(cls, **kwargs):
kwargs["number"] = get_number()
kwargs["pydantic_test"] = PydanticTest(aa="random", bb=42)
return super().construct(**kwargs)

id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=200)
url: HttpUrl = "https://www.example3.com" # type: ignore
Expand Down