MCPcopy
hub / github.com/ormar-orm/ormar / _construct_relations

Method _construct_relations

ormar/models/newbasemodel.py:1237–1257  ·  view source on GitHub ↗
(cls: type["T"], model: "T", values: builtins.dict)

Source from the content-addressed store, hash-verified

1235
1236 @classmethod
1237 def _construct_relations(cls: type["T"], model: "T", values: builtins.dict) -> None:
1238 present_relations = [
1239 relation for relation in cls.extract_related_names() if relation in values
1240 ]
1241 for relation in present_relations:
1242 value_to_set = values[relation]
1243 if not isinstance(value_to_set, list):
1244 value_to_set = [value_to_set]
1245 relation_field = cls.ormar_config.model_fields[relation]
1246 relation_value = [
1247 relation_field.expand_relationship(x, model, to_register=False)
1248 for x in value_to_set
1249 if x is not None
1250 ]
1251
1252 for child in relation_value:
1253 model._orm.add(
1254 parent=cast("Model", child),
1255 child=cast("Model", model),
1256 field=cast("ForeignKeyField", relation_field),
1257 )
1258
1259 def update_from_dict(self, value_dict: builtins.dict) -> "NewBaseModel":
1260 """

Callers 1

model_constructMethod · 0.80

Calls 3

extract_related_namesMethod · 0.80
expand_relationshipMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected