From c1b221a9b913315998a1bcec2f29a9361a74d1ac Mon Sep 17 00:00:00 2001 From: Adam Chainz Date: Fri, 23 Dec 2016 15:58:06 +0000 Subject: [PATCH] Fixed #27625 -- Made LazySettings cache attributes in __dict__. --- django/conf/__init__.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/django/conf/__init__.py b/django/conf/__init__.py index 4fec2ee646..f99236a778 100644 --- a/django/conf/__init__.py +++ b/django/conf/__init__.py @@ -49,9 +49,32 @@ class LazySettings(LazyObject): } def __getattr__(self, name): + """ + Return the value of a setting and cache it in self.__dict__. + """ if self._wrapped is empty: self._setup(name) - return getattr(self._wrapped, name) + val = getattr(self._wrapped, name) + self.__dict__[name] = val + return val + + def __setattr__(self, name, value): + """ + Set the value of setting. Clear all cached values if _wrapped changes + (@override_settings does this) or clear single values when set. + """ + if name == '_wrapped': + self.__dict__.clear() + else: + self.__dict__.pop(name, None) + super(LazySettings, self).__setattr__(name, value) + + def __delattr__(self, name): + """ + Delete a setting and clear it from cache if needed. + """ + super(LazySettings, self).__delattr__(name) + self.__dict__.pop(name, None) def configure(self, default_settings=global_settings, **options): """