From 2647ee7e639553dabaaf2cd13b63958b0cd82a93 Mon Sep 17 00:00:00 2001 From: linchiwei123 <306741005@qq.com> Date: Fri, 28 Jun 2019 14:29:38 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=C2=96=C2=96make=20use=20of=20collections.C?= =?UTF-8?q?hainMap=20to=20simplify=20the=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- django/db/models/query_utils.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/django/db/models/query_utils.py b/django/db/models/query_utils.py index 90289d0da232..0eb8a51c640f 100644 --- a/django/db/models/query_utils.py +++ b/django/db/models/query_utils.py @@ -8,7 +8,7 @@ import copy import functools import inspect -from collections import namedtuple +from collections import ChainMap, namedtuple from django.db.models.constants import LOOKUP_SEP from django.utils import tree @@ -187,10 +187,7 @@ def merge_dicts(dicts): Merge dicts in reverse to preference the order of the original list. e.g., merge_dicts([a, b]) will preference the keys in 'a' over those in 'b'. """ - merged = {} - for d in reversed(dicts): - merged.update(d) - return merged + return dict(ChainMap(*dicts)) @classmethod def _clear_cached_lookups(cls): From b532f1300694ae344bf8bc10b5a36124a6b10987 Mon Sep 17 00:00:00 2001 From: linchiwei123 <306741005@qq.com> Date: Fri, 28 Jun 2019 16:38:12 +0800 Subject: [PATCH 2/2] use ChainMap() instead of dict(ChainMap()) --- django/db/models/query_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django/db/models/query_utils.py b/django/db/models/query_utils.py index 0eb8a51c640f..4966e2ce12d4 100644 --- a/django/db/models/query_utils.py +++ b/django/db/models/query_utils.py @@ -187,7 +187,7 @@ def merge_dicts(dicts): Merge dicts in reverse to preference the order of the original list. e.g., merge_dicts([a, b]) will preference the keys in 'a' over those in 'b'. """ - return dict(ChainMap(*dicts)) + return ChainMap(*dicts) @classmethod def _clear_cached_lookups(cls):