From a9f88e8f8f535a4b9964a393e646f5c82ab8588c Mon Sep 17 00:00:00 2001 From: collerek Date: Wed, 26 Aug 2020 14:33:46 +0200 Subject: [PATCH] rename alias manager --- .coverage | Bin 53248 -> 53248 bytes docs/relations.md | 8 ++++---- ormar/fields/foreign_key.py | 10 +--------- ormar/models/metaclass.py | 4 ++-- ormar/models/model.py | 2 +- ormar/queryset/clause.py | 2 +- ormar/queryset/query.py | 4 ++-- ormar/relations.py | 1 - 8 files changed, 11 insertions(+), 20 deletions(-) diff --git a/.coverage b/.coverage index 0cd63857c69e6da9ef7ec5d430768f9a355b2980..a8c10f89578082e93b09966ce034e9233c94b2b1 100644 GIT binary patch delta 220 zcmV<203-i^paX!Q1F$783N|1yG&(agIx;u2FE7_nHpu`F`48$3-Vf6c$Pc&=p$~Wu za1T!pGY=~d4i4}R)(*T5rw)=1iVl1ZPYyB;4Gs4V;tk2O5fF_Hlgo|LJ8ib_o4@Ya zfA6>N0}~Mh0SPD(?qc%(+uy(aZ}0o<_g)wf1OW*$5Pno=2ck1-b&2Lu5LEC=f1 WP4@r)lke_(e;@b!o(HqIk4Qke*Ij=A delta 204 zcmV;-05ku9paX!Q1F$783N#=wF*-3cIxsP_FE7_nHp>7H`48$3-Vf6c$Pc&=pbvKs zZx2roGY=~d4i4}R)(*Z7sScD5j1GPdQ4TZ?4-NSZ diff --git a/docs/relations.md b/docs/relations.md index c244673..3edf120 100644 --- a/docs/relations.md +++ b/docs/relations.md @@ -79,13 +79,13 @@ But don't worry - ormar can handle situations like this, as it uses the Relation Each class is registered with the same instance of the AliasManager that you can access like this: ```python -SchoolClass._orm_relationship_manager +SchoolClass.alias_manager ``` It's the same object for all `Models` ```python -print(Teacher._orm_relationship_manager == Student._orm_relationship_manager) +print(Teacher.alias_manager == Student.alias_manager) # will produce: True ``` @@ -94,11 +94,11 @@ print(Teacher._orm_relationship_manager == Student._orm_relationship_manager) You can even preview the alias used for any relation by passing two tables names. ```python -print(Teacher._orm_relationship_manager.resolve_relation_join( +print(Teacher.alias_manager.resolve_relation_join( 'students', 'categories')) # will produce: KId1c6 (sample value) -print(Teacher._orm_relationship_manager.resolve_relation_join( +print(Teacher.alias_manager.resolve_relation_join( 'categories', 'students')) # will produce: EFccd5 (sample value) ``` diff --git a/ormar/fields/foreign_key.py b/ormar/fields/foreign_key.py index deea69b..9fad3fe 100644 --- a/ormar/fields/foreign_key.py +++ b/ormar/fields/foreign_key.py @@ -54,6 +54,7 @@ def ForeignKey( class ForeignKeyField(BaseField): to: Type["Model"] + name: str related_name: str virtual: bool @@ -65,15 +66,6 @@ class ForeignKeyField(BaseField): def validate(cls, value: Any) -> Any: return value - # @property - # def __type__(self) -> Type[BaseModel]: - # return self.to.__pydantic_model__ - - # @classmethod - # def get_column_type(cls) -> sqlalchemy.Column: - # to_column = cls.to.Meta.model_fields[cls.to.Meta.pkname] - # return to_column.column_type - @classmethod def _extract_model_from_sequence( cls, value: List, child: "Model" diff --git a/ormar/models/metaclass.py b/ormar/models/metaclass.py index 43fc0ca..1332b33 100644 --- a/ormar/models/metaclass.py +++ b/ormar/models/metaclass.py @@ -26,7 +26,7 @@ class ModelMeta: columns: List[sqlalchemy.Column] pkname: str model_fields: Dict[str, Union[BaseField, ForeignKey]] - _orm_relationship_manager: AliasManager + alias_manager: AliasManager def register_relation_on_build(table_name: str, field: ForeignKey, name: str) -> None: @@ -158,7 +158,7 @@ class ModelMetaclass(pydantic.main.ModelMetaclass): ) expand_reverse_relationships(new_model) - new_model.Meta._orm_relationship_manager = relationship_manager + new_model.Meta.alias_manager = relationship_manager new_model.objects = QuerySet(new_model) return new_model diff --git a/ormar/models/model.py b/ormar/models/model.py index 8be4ca9..338b01a 100644 --- a/ormar/models/model.py +++ b/ormar/models/model.py @@ -40,7 +40,7 @@ class Model(NewBaseModel): if select_related: related_models = group_related_list(select_related) - table_prefix = cls.Meta._orm_relationship_manager.resolve_relation_join( + table_prefix = cls.Meta.alias_manager.resolve_relation_join( previous_table, cls.Meta.table.name ) diff --git a/ormar/queryset/clause.py b/ormar/queryset/clause.py index dc94e6f..01a181a 100644 --- a/ormar/queryset/clause.py +++ b/ormar/queryset/clause.py @@ -109,7 +109,7 @@ class QueryClause: previous_table = model_cls.Meta.tablename for part in related_parts: current_table = model_cls.Meta.model_fields[part].to.Meta.tablename - manager = model_cls.Meta._orm_relationship_manager + manager = model_cls.Meta.alias_manager table_prefix = manager.resolve_relation_join(previous_table, current_table) model_cls = model_cls.Meta.model_fields[part].to previous_table = current_table diff --git a/ormar/queryset/query.py b/ormar/queryset/query.py index 9ab8638..95b2ff1 100644 --- a/ormar/queryset/query.py +++ b/ormar/queryset/query.py @@ -44,7 +44,7 @@ class Query: @property def relation_manager(self) -> AliasManager: - return self.model_cls.Meta._orm_relationship_manager + return self.model_cls.Meta.alias_manager def build_select_expression(self) -> Tuple[sqlalchemy.sql.select, List[str]]: self.columns = list(self.table.columns) @@ -84,7 +84,7 @@ class Query: model_cls = join_params.model_cls.Meta.model_fields[part].to to_table = model_cls.Meta.table.name - alias = model_cls.Meta._orm_relationship_manager.resolve_relation_join( + alias = model_cls.Meta.alias_manager.resolve_relation_join( join_params.from_table, to_table ) if alias not in self.used_aliases: diff --git a/ormar/relations.py b/ormar/relations.py index df3dd26..8909140 100644 --- a/ormar/relations.py +++ b/ormar/relations.py @@ -1,5 +1,4 @@ import string -import string import uuid from enum import Enum from random import choices