Files
ormar/docs/api/relations/relation-manager.md

3.7 KiB

relations.relation_manager

RelationsManager Objects

class RelationsManager()

Manages relations on a Model, each Model has it's own instance.

__contains__

 | __contains__(item: str) -> bool

Checks if relation with given name is already registered.

Arguments:

  • item (str): name of attribute

Returns:

(bool): result of the check

get

 | get(name: str) -> Optional[Union["Model", Sequence["Model"]]]

Returns the related model/models if relation is set. Actual call is delegated to Relation instance registered under relation name.

Arguments:

  • name (str): name of the relation

Returns:

(Optional[Union[Model, List[Model]]): related model or list of related models if set

add

 | @staticmethod
 | add(parent: "Model", child: "Model", field: Type["ForeignKeyField"]) -> None

Adds relation on both sides -> meaning on both child and parent models. One side of the relation is always weakref proxy to avoid circular refs.

Based on the side from which relation is added and relation name actual names of parent and child relations are established. The related models are registered on both ends.

Arguments:

  • parent (Model): parent model on which relation should be registered
  • child (Model): child model to register
  • field (ForeignKeyField): field with relation definition

remove

 | remove(name: str, child: Union["NewBaseModel", Type["NewBaseModel"]]) -> None

Removes given child from relation with given name. Since you can have many relations between two models you need to pass a name of relation from which you want to remove the child.

Arguments:

  • name (str): name of the relation
  • child (Union[Model, Type[Model]]): child to remove from relation

remove_parent

 | @staticmethod
 | remove_parent(item: Union["NewBaseModel", Type["NewBaseModel"]], parent: "Model", name: str) -> None

Removes given parent from relation with given name. Since you can have many relations between two models you need to pass a name of relation from which you want to remove the parent.

Arguments:

  • item (Union[Model, Type[Model]]): model with parent registered
  • parent (Model): parent Model
  • name (str): name of the relation

_get

 | _get(name: str) -> Optional[Relation]

Returns the actual relation and not the related model(s).

Arguments:

  • name (str): name of the relation

Returns:

(ormar.relations.relation.Relation): Relation instance

_get_relation_type

 | _get_relation_type(field: Type["BaseField"]) -> RelationType

Returns type of the relation declared on a field.

Arguments:

  • field (Type[BaseField]): field with relation declaration

Returns:

(RelationType): type of the relation defined on field

_add_relation

 | _add_relation(field: Type["BaseField"]) -> None

Registers relation in the manager. Adds Relation instance under field.name.

Arguments:

  • field (Type[BaseField]): field with relation declaration