Skip to content

models.helpers.relations

register_relation_on_build

1
register_relation_on_build(field: "ForeignKeyField") -> None

Registers ForeignKey relation in alias_manager to set a table_prefix. Registration include also reverse relation side to be able to join both sides.

Relation is registered by model name and relation field name to allow for multiple relations between two Models that needs to have different aliases for proper sql joins.

Arguments:

  • field: relation field :type field: ForeignKey class

register_many_to_many_relation_on_build

1
register_many_to_many_relation_on_build(field: "ManyToManyField") -> None

Registers connection between through model and both sides of the m2m relation. Registration include also reverse relation side to be able to join both sides.

Relation is registered by model name and relation field name to allow for multiple relations between two Models that needs to have different aliases for proper sql joins.

By default relation name is a model.name.lower().

Arguments:

  • field: relation field :type field: ManyToManyField class

expand_reverse_relationship

1
expand_reverse_relationship(model_field: "ForeignKeyField") -> None

If the reverse relation has not been set before it's set here.

Arguments:

  • model_field: :type model_field:

Returns:

None :rtype: None

expand_reverse_relationships

1
expand_reverse_relationships(model: Type["Model"]) -> None

Iterates through model_fields of given model and verifies if all reverse relation have been populated on related models.

If the reverse relation has not been set before it's set here.

Arguments:

  • model: model on which relation should be checked and registered :type model: Model class

register_reverse_model_fields

1
register_reverse_model_fields(model_field: "ForeignKeyField") -> None

Registers reverse ForeignKey field on related model. By default it's name.lower()+'s' of the model on which relation is defined.

But if the related_model name is provided it's registered with that name. Autogenerated reverse fields also set related_name to the original field name.

Arguments:

  • model_field: original relation ForeignKey field :type model_field: relation Field

register_through_shortcut_fields

1
register_through_shortcut_fields(model_field: "ManyToManyField") -> None

Registers m2m relation through shortcut on both ends of the relation.

Arguments:

  • model_field: relation field defined in parent model :type model_field: ManyToManyField

register_relation_in_alias_manager

1
register_relation_in_alias_manager(field: "ForeignKeyField") -> None

Registers the relation (and reverse relation) in alias manager. The m2m relations require registration of through model between actual end models of the relation.

Delegates the actual registration to: m2m - register_many_to_many_relation_on_build fk - register_relation_on_build

Arguments:

  • field: relation field :type field: ForeignKey or ManyToManyField class

1
verify_related_name_dont_duplicate(related_name: str, model_field: "ForeignKeyField") -> None

Verifies whether the used related_name (regardless of the fact if user defined or auto generated) is already used on related model, but is connected with other model than the one that we connect right now.

Raises:

  • ModelDefinitionError: if name is already used but lead to different related model

Arguments:

  • related_name: :type related_name:
  • model_field: original relation ForeignKey field :type model_field: relation Field

Returns:

None :rtype: None

reverse_field_not_already_registered

1
reverse_field_not_already_registered(model_field: "ForeignKeyField") -> bool

Checks if child is already registered in parents pydantic fields.

Raises:

  • ModelDefinitionError: if related name is already used but lead to different related model

Arguments:

  • model_field: original relation ForeignKey field :type model_field: relation Field

Returns:

result of the check :rtype: bool