add tests for cross model forward references, add docs for processing forwardrefs, wip on refactoring queries into separate pages based on functionality

This commit is contained in:
collerek
2021-01-26 17:29:40 +01:00
parent a2834666fc
commit b710ed9780
39 changed files with 2054 additions and 1004 deletions

View File

@ -124,7 +124,7 @@ class ForeignKeyConstraint:
def ForeignKey( # noqa CFQ002
to: Union[Type["Model"]],
to: Union[Type["Model"], "ForwardRef"],
*,
name: str = None,
unique: bool = False,

View File

@ -36,8 +36,8 @@ def populate_m2m_params_based_on_to_model(
def ManyToMany(
to: Type["Model"],
through: Type["Model"],
to: Union[Type["Model"], ForwardRef],
through: Union[Type["Model"], ForwardRef],
*,
name: str = None,
unique: bool = False,
@ -77,7 +77,7 @@ def ManyToMany(
column_type = None
else:
__type__, column_type = populate_m2m_params_based_on_to_model(
to=to, nullable=nullable
to=to, nullable=nullable # type: ignore
)
namespace = dict(
__type__=__type__,
@ -164,12 +164,20 @@ class ManyToManyField(ForeignKeyField, ormar.QuerySetProtocol, ormar.RelationPro
:return: None
:rtype: None
"""
if cls.to.__class__ == ForwardRef or cls.through.__class__ == ForwardRef:
if cls.to.__class__ == ForwardRef:
cls.to = evaluate_forwardref(
cls.to, # type: ignore
globalns,
localns or None,
)
(cls.__type__, cls.column_type,) = populate_m2m_params_based_on_to_model(
to=cls.to, nullable=cls.nullable,
)
if cls.through.__class__ == ForwardRef:
cls.through = evaluate_forwardref(
cls.through, # type: ignore
globalns,
localns or None,
)

View File

@ -110,7 +110,6 @@ class Model(NewBaseModel):
previous_model = through_field.through # type: ignore
if previous_model and rel_name2:
# TODO finish duplicated nested relation or remove this
if current_relation_str and "__" in current_relation_str and source_model:
table_prefix = cls.Meta.alias_manager.resolve_relation_alias(
from_model=source_model, relation_name=current_relation_str
@ -167,6 +166,10 @@ class Model(NewBaseModel):
Recurrently calls from_row method on nested instances and create nested
instances. In the end those instances are added to the final model dictionary.
:param source_model: source model from which relation started
:type source_model: Type[Model]
:param current_relation_str: joined related parts into one string
:type current_relation_str: str
:param item: dictionary of already populated nested models, otherwise empty dict
:type item: Dict
:param row: raw result row from the database

View File

@ -228,9 +228,6 @@ class QuerySet:
:return: filtered QuerySet
:rtype: QuerySet
"""
# TODO: delay processing of filter clauses or switch to group one
# that keeps all aliases even if duplicated - now initialized too late
# in the join
qryclause = QueryClause(
model_cls=self.model,
select_related=self._select_related,