From 9fcd7b8eeff2e2b6a109f6a1a7b4b80a07c40a2f Mon Sep 17 00:00:00 2001 From: collerek Date: Thu, 1 Apr 2021 17:40:38 +0200 Subject: [PATCH] fix translating to dict lists with same name of relation but different target models --- docs/releases.md | 1 + ormar/queryset/utils.py | 9 +++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/docs/releases.md b/docs/releases.md index 6db4463..e52d34f 100644 --- a/docs/releases.md +++ b/docs/releases.md @@ -24,6 +24,7 @@ * Fix improper relation field resolution in `QuerysetProxy` if fk column has different database alias. * Fix hitting recursion error with very complicated models structure with loops when calling `dict()`. * Fix bug when two non-relation fields were merged (appended) in query result when they were not relation fields (i.e. JSON) +* Fix bug when during translation to dict from list the same relation name is used in chain but leads to different models ## Other diff --git a/ormar/queryset/utils.py b/ormar/queryset/utils.py index 3ed7f93..dc24fd9 100644 --- a/ormar/queryset/utils.py +++ b/ormar/queryset/utils.py @@ -18,7 +18,7 @@ if TYPE_CHECKING: # pragma no cover def check_node_not_dict_or_not_last_node( - part: str, parts: List, current_level: Any + part: str, is_last: bool, current_level: Any ) -> bool: """ Checks if given name is not present in the current level of the structure. @@ -36,7 +36,7 @@ def check_node_not_dict_or_not_last_node( :return: result of the check :rtype: bool """ - return (part not in current_level and part != parts[-1]) or ( + return (part not in current_level and not is_last) or ( part in current_level and not isinstance(current_level[part], dict) ) @@ -71,9 +71,10 @@ def translate_list_to_dict( # noqa: CCR001 else: def_val = "asc" - for part in parts: + for ind, part in enumerate(parts): + is_last = ind == len(parts) - 1 if check_node_not_dict_or_not_last_node( - part=part, parts=parts, current_level=current_level + part=part, is_last=is_last, current_level=current_level ): current_level[part] = dict() elif part not in current_level: