4.9 KiB
models.helpers.relations
register_relation_on_build
register_relation_on_build(new_model: Type["Model"], field_name: str) -> 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:
new_model (Model class): constructed modelfield_name (str): name of the related field
register_many_to_many_relation_on_build
register_many_to_many_relation_on_build(new_model: Type["Model"], field: Type[ManyToManyField], field_name: str) -> 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_name (str): name of the relation keynew_model (Model class): model on which m2m field is declaredfield (ManyToManyField class): relation field
expand_reverse_relationships
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 class): model on which relation should be checked and registered
register_reverse_model_fields
register_reverse_model_fields(model: Type["Model"], child: Type["Model"], related_name: str, model_field: Type["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 (Model class): related model on which reverse field should be definedchild (Model class): parent model with relation definitionrelated_name (str): name by which reverse key should be registeredmodel_field (relation Field): original relation ForeignKey field
register_relation_in_alias_manager
register_relation_in_alias_manager(new_model: Type["Model"], field: Type[ForeignKeyField], field_name: str) -> 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:
new_model (Model class): model on which relation field is declaredfield (ForeignKey or ManyToManyField class): relation fieldfield_name (str): name of the relation key
verify_related_name_dont_duplicate
verify_related_name_dont_duplicate(child: Type["Model"], parent_model: Type["Model"], related_name: str) -> 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:
child (ormar.models.metaclass.ModelMetaclass): related Model classparent_model (ormar.models.metaclass.ModelMetaclass): parent Model classrelated_name ():
Returns:
(None): None
reverse_field_not_already_registered
reverse_field_not_already_registered(child: Type["Model"], child_model_name: str, parent_model: Type["Model"]) -> 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:
child (ormar.models.metaclass.ModelMetaclass): related Model classchild_model_name (str): related_name of the child if providedparent_model (ormar.models.metaclass.ModelMetaclass): parent Model class
Returns:
(bool): result of the check