switch to expand relationship to allow pk values as models

This commit is contained in:
collerek
2021-10-09 20:21:22 +02:00
parent 127df1e9cb
commit 3a0fba5a60

View File

@ -823,25 +823,17 @@ class NewBaseModel(pydantic.BaseModel, ModelTableProxy, metaclass=ModelMetaclass
value_to_set = [value_to_set]
relation_field = cls.Meta.model_fields[relation]
relation_value = [
cls.construct_from_dict_if_required(relation_field, value=x)
relation_field.expand_relationship(x, model, to_register=False)
for x in value_to_set
]
for child in relation_value:
model._orm.add(
parent=child,
parent=cast("Model", child),
child=cast("Model", model),
field=cast("ForeignKeyField", relation_field),
)
@staticmethod
def construct_from_dict_if_required(
relation_field: "BaseField", value: Any
) -> "Model":
return (
relation_field.to.construct(**value) if isinstance(value, dict) else value
)
def update_from_dict(self, value_dict: Dict) -> "NewBaseModel":
"""
Updates self with values of fields passed in the dictionary.