Skip to content

relations.relation_manager

RelationsManager Objects

1
class RelationsManager()

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

__contains__

1
 | __contains__(item: str) -> bool

Checks if relation with given name is already registered.

Arguments:

  • item: name of attribute :type item: str

Returns:

result of the check :rtype: bool

get

1
 | 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: name of the relation :type name: str

Returns:

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

add

1
2
 | @staticmethod
 | add(parent: "Model", child: "Model", field: "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: parent model on which relation should be registered :type parent: Model
  • child: child model to register :type child: Model
  • field: field with relation definition :type field: ForeignKeyField

remove

1
 | 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: name of the relation :type name: str
  • child: child to remove from relation :type child: Union[Model, Type[Model]]

remove_parent

1
2
 | @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: model with parent registered :type item: Union[Model, Type[Model]]
  • parent: parent Model :type parent: Model
  • name: name of the relation :type name: str

_get

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

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

Arguments:

  • name: name of the relation :type name: str

Returns:

Relation instance :rtype: ormar.relations.relation.Relation

_get_relation_type

1
 | _get_relation_type(field: "BaseField") -> RelationType

Returns type of the relation declared on a field.

Arguments:

  • field: field with relation declaration :type field: BaseField

Returns:

type of the relation defined on field :rtype: RelationType

_add_relation

1
 | _add_relation(field: "BaseField") -> None

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

Arguments:

  • field: field with relation declaration :type field: BaseField