Mutating settings at runtime and in tests
Why does assigning to a setting leave some Django subsystems holding stale values, and how does override_settings notify the rest of the framework?
Once LazySettings has evaluated, settings feels like an ordinary mutable Python object. In a test suite — or a quick debugging script — it looks completely natural to write settings and expect Django to switch timezones. Python accepts the assignment without complaint, and reading settings back prints "America/.
Yet half of Django never saw the change.
Direct assignment versus override_settings
Run both approaches back to back on our booted newsroom project and watch what django actually returns:
override_settings, setting_changed, and stale captured state
initial default timezone -> 'UTC'
TRAP after direct settings.TIME_ZONE = 'America/Chicago': timezone.get_default_timezone_name() -> 'UTC' (stale lru_cache)
inside override_settings: type(_wrapped)=UserSettingsHolder timezone='America/Chicago'
after exit: type(_wrapped)=Settings timezone='UTC'
setting_changed(setting='TIME_ZONE', value='America/Chicago', enter=True)
setting_changed(setting='TIME_ZONE', value='UTC', enter=False)
TRAP override_settings(DATABASES=...): UserWarning: Overriding setting DATABASES can lead to unexpected behavior.
TRAP override_settings(MIDDLEWARE=[]): existing WSGIHandler._middleware_chain unchanged=True; fresh WSGIHandler chain differs=True
When you execute settings directly, LazySettings pops "TIME_ZONE" from the proxy’s __dict__ and writes the new string onto LazyObject. Nothing else in the process is told. django is decorated with functools, so it keeps handing back its cached "UTC" zone info. The exact same quiet staleness affects cached template engines, database routers, storage backends, cache handlers, password validators, and compiled URL resolvers.
How override_settings resets the framework
override_settings avoids mutating the underlying Settings object and notifies every listening subsystem inside override_settings.
If "INSTALLED_APPS" is among the overridden keys, enable updates the app registry first via Apps, rolling back with Apps if an invalid app list raises an error so settings stays clean. Next it wraps the current _wrapped object inside a UserSettingsHolder, attaches the overridden attributes to that wrapper, saves the previous _wrapped, and assigns the new wrapper to LazyObject on settings.
Reassigning _wrapped triggers a special branch at the top of LazySettings: if name == "_wrapped": self. Every cached setting on the proxy vanishes in one stroke so subsequent reads resolve through the new UserSettingsHolder layer.
Then Django broadcasts setting_changed for every overridden key with enter=True. When the with block or test finishes, override_settings restores the saved _wrapped (flushing __dict__ a second time) and sends setting_changed with enter=False using Signal so one failing receiver cannot prevent later receivers from cleaning up.
Look inside django/ to see how many caches across Django have to be unwound every time a setting changes: update_connections_time_zone clears get_default_timezone and resets active database connections’ timezones, clear_cache_handlers closes and rebuilds caches, reset_template_engines clears engines and form renderers, root_urlconf_changed clears URL resolver caches, and user_model_swapped even reaches into five auth modules to rebind cached module-level django globals.
Where even override_settings cannot reach
Even with a dozen built-in setting_changed receivers, three boundaries stay unreachable.
First, any module in your own codebase that wrote TIMEOUT = settings at import time bound an ordinary Python integer into its module namespace forever. No setting_changed receiver knows that module exists; either read settings when the value is used or connect your own setting_changed listener.
Second, overriding "DATABASES" triggers complex_setting_changed and emits a UserWarning, as our recording confirmed on line 154.
Third — and easiest to miss — look at the final line of our recording:
override_settings, setting_changed, and stale captured state
...
TRAP override_settings(MIDDLEWARE=[]): existing WSGIHandler._middleware_chain unchanged=True; fresh WSGIHandler chain differs=True
When a WSGIHandler is instantiated, WSGIHandler calls BaseHandler once and compiles settings into a closed-over chain of callables stored on BaseHandler. Changing MIDDLEWARE via override_settings does not rebuild any handler that already exists — only constructing a fresh handler picks up the new list (which is why Django’s test Client builds a fresh handler on requests when middleware changes, whereas a long-lived WSGIHandler instance does not).
Settings are now configured, all eight apps are populated, every model is registered, cross-app foreign keys are wired, and every ready() hook has run. The booted process is finally ready for a web server to construct a handler — and compile that MIDDLEWARE list into the nested chain that wraps every incoming request. That machinery is where Handlers, middleware, and async begins.