Skip to content

relations.relation

RelationType Objects

1
class RelationType(Enum)

Different types of relations supported by ormar:

  • ForeignKey = PRIMARY
  • reverse ForeignKey = REVERSE
  • ManyToMany = MULTIPLE

Relation Objects

1
class Relation(Generic[T])

Keeps related Models and handles adding/removing of the children.

__init__

1
 | __init__(manager: "RelationsManager", type_: RelationType, field_name: str, to: Type["T"], through: Type["Model"] = None) -> None

Initialize the Relation and keep the related models either as instances of passed Model, or as a RelationProxy which is basically a list of models with some special behavior, as it exposes QuerySetProxy and allows querying the related models already pre filtered by parent model.

Arguments:

  • manager: reference to relation manager :type manager: RelationsManager
  • type_: type of the relation :type type_: RelationType
  • field_name: name of the relation field :type field_name: str
  • to: model to which relation leads to :type to: Type[Model]
  • through: model through which relation goes for m2m relations :type through: Type[Model]

1
 | _clean_related() -> None

Removes dead weakrefs from RelationProxy.

_find_existing

1
 | _find_existing(child: Union["NewBaseModel", Type["NewBaseModel"]]) -> Optional[int]

Find child model in RelationProxy if exists.

Arguments:

  • child: child model to find :type child: Model

Returns:

index of child in RelationProxy :rtype: Optional[ind]

add

1
 | add(child: "Model") -> None

Adds child Model to relation, either sets child as related model or adds it to the list in RelationProxy depending on relation type.

Arguments:

  • child: model to add to relation :type child: Model

remove

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

Removes child Model from relation, either sets None as related model or removes it from the list in RelationProxy depending on relation type.

Arguments:

  • child: model to remove from relation :type child: Model

get

1
 | get() -> Optional[Union[List["Model"], "Model"]]

Return the related model or models from RelationProxy.

Returns:

related model/models if set :rtype: Optional[Union[List[Model], Model]]