diff options
| author | Claude Paroz <claude@2xlibre.net> | 2014-08-29 14:54:08 +0200 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2014-08-30 12:37:10 +0200 |
| commit | 66757fee7e921ad4c35e0b3f80c25e026100b31c (patch) | |
| tree | 0b6a6e10a1d800532d114ab4b5b4a2d6f7d73abd /django/utils/datastructures.py | |
| parent | 05a8cef4288e2a85dbaff12bc2683a75c9998618 (diff) | |
Fixed #23384 -- Allowed overriding part of a dictionary-type setting
This change is needed for upcoming changes where settings might be
grouped in a parent dictionary.
Thanks Tim Graham for the review.
Diffstat (limited to 'django/utils/datastructures.py')
| -rw-r--r-- | django/utils/datastructures.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py index 2efcd00eb0..ad8792d192 100644 --- a/django/utils/datastructures.py +++ b/django/utils/datastructures.py @@ -244,6 +244,26 @@ class SortedDict(dict): self.keyOrder = [] +def dict_merge(a, b): + """ + Utility to recursively merge two dicts, taking care not to overwrite subkeys + (which would happen with dict.update), but keeping existing key including + those from subdictionaries (optionally opted-out if a `_clear_defaults` key + is present). + Thanks Ross McFarland (https://www.xormedia.com/recursively-merge-dictionaries-in-python/) + """ + if b.get('_clear_defaults'): + return copy.deepcopy(b) + + result = copy.deepcopy(a) + for key, value in six.iteritems(b): + if key in a and isinstance(result[key], dict): + result[key] = dict_merge(result[key], value) + else: + result[key] = value + return result + + class OrderedSet(object): """ A set which keeps the ordering of the inserted items. |
