Skip to content

fields.many_to_many

forbid_through_relations

1
forbid_through_relations(through: Type["Model"]) -> None

Verifies if the through model does not have relations.

Arguments:

  • through: through Model to be checked :type through: Type['Model]

populate_m2m_params_based_on_to_model

1
populate_m2m_params_based_on_to_model(to: Type["Model"], nullable: bool) -> Tuple[Any, Any]

Based on target to model to which relation leads to populates the type of the pydantic field to use and type of the target column field.

Arguments:

  • to: target related ormar Model :type to: Model class
  • nullable: marks field as optional/ required :type nullable: bool

Returns:

Tuple[List, Any] :rtype: tuple with target pydantic type and target col type

ManyToMany

1
ManyToMany(to: "ToType", through: Optional["ToType"] = None, *, name: str = None, unique: bool = False, virtual: bool = False, **kwargs: Any, ,) -> "RelationProxy[T]"

Despite a name it's a function that returns constructed ManyToManyField. This function is actually used in model declaration (as ormar.ManyToMany(ToModel, through=ThroughModel)).

Accepts number of relation setting parameters as well as all BaseField ones.

Arguments:

  • to: target related ormar Model :type to: Model class
  • through: through model for m2m relation :type through: Model class
  • name: name of the database field - later called alias :type name: str
  • unique: parameter passed to sqlalchemy.ForeignKey, unique flag :type unique: bool
  • virtual: marks if relation is virtual. It is for reversed FK and auto generated FK on through model in Many2Many relations. :type virtual: bool
  • kwargs: all other args to be populated by BaseField :type kwargs: Any

Returns:

ormar ManyToManyField with m2m relation to selected model :rtype: ManyToManyField

ManyToManyField Objects

1
class ManyToManyField(ForeignKeyField,  ormar.QuerySetProtocol,  ormar.RelationProtocol)

Actual class returned from ManyToMany function call and stored in model_fields.

1
 | get_source_related_name() -> str

Returns name to use for source relation name. For FK it's the same, differs for m2m fields. It's either set as related_name or by default it's field name.

Returns:

name of the related_name or default related name. :rtype: str

has_unresolved_forward_refs

1
 | has_unresolved_forward_refs() -> bool

Verifies if the filed has any ForwardRefs that require updating before the model can be used.

Returns:

result of the check :rtype: bool

evaluate_forward_ref

1
 | evaluate_forward_ref(globalns: Any, localns: Any) -> None

Evaluates the ForwardRef to actual Field based on global and local namespaces

Arguments:

  • globalns: global namespace :type globalns: Any
  • localns: local namespace :type localns: Any

Returns:

None :rtype: None

get_relation_name

1
 | get_relation_name() -> str

Returns name of the relation, which can be a own name or through model names for m2m models

Returns:

result of the check :rtype: bool

get_source_model

1
 | get_source_model() -> Type["Model"]

Returns model from which the relation comes -> either owner or through model

Returns:

source model :rtype: Type["Model"]

create_default_through_model

1
 | create_default_through_model() -> None

Creates default empty through model if no additional fields are required.