[Django] #37071: BaseConnectionHandler uses @cached_property and private attribute for settings

3 views
Skip to first unread message

Django

unread,
Apr 27, 2026, 7:53:58 AM (4 days ago) Apr 27
to django-...@googlegroups.com
#37071: BaseConnectionHandler uses @cached_property and private attribute for
settings
-------------------------------+--------------------------------------
Reporter: Michael Foley | Type: Bug
Status: new | Component: Utilities
Version: 6.0 | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
While working on setting up a Class based backend setup for a project I
was having some issues with resetting the backends I was using during
testing. When using override_settings the the django settings themselves
would override but the backend wouldn't change based on those settings.

After a bit more digging I found the settings property on the
BaseConnectionHandler
https://github.com/django/django/blob/017d7f6f12e597e6179de7ffdf330a52c2b22053/django/utils/connection.py#L44

Seeing it was a cached property I followed the reset process outlined in
the utillty docs
https://docs.djangoproject.com/en/6.0/ref/utils/

To clear the cached result using

{{{
handler.__dict__.pop("settings", None)
}}}

But this still didn't seem to clear the settings as expected.

Looking a bit further I found that the BaseConnectionHandler also has a
private property of _settings that is also setup as a cache for the
settings

https://github.com/django/django/blob/017d7f6f12e597e6179de7ffdf330a52c2b22053/django/utils/connection.py#L39
{{{
def __init__(self, settings=None):
self._settings = settings
self._connections = Local(self.thread_critical)

@cached_property
def settings(self):
self._settings = self.configure_settings(self._settings)
return self._settings

def configure_settings(self, settings):
if settings is None:
settings = getattr(django_settings, self.settings_name)
return settings
}}}


So it looks like both @cached_property and a private property are being
maintained as caches for the settings.

Updating my reset process to clear the cached property and the _settings
property on the backend fixed the testing and the backends would switch
without a problem.

I wasn't sure if this was an intentional setup or to support backwards
compatibility but it did create a bit of hair pulling as while I was
working on it. git blame shows its been there since the connection broker
was initially setup.
--
Ticket URL: <https://code.djangoproject.com/ticket/37071>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 29, 2026, 3:24:56 PM (yesterday) Apr 29
to django-...@googlegroups.com
#37071: BaseConnectionHandler uses @cached_property and private attribute for
settings
-------------------------------+--------------------------------------
Reporter: Michael Foley | Owner: (none)
Type: Bug | Status: closed
Component: Utilities | Version: 6.0
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Jacob Walls):

* resolution: => wontfix
* status: new => closed

Comment:

Thanks for the writeup. In the absence of a more compelling benefit, I
think we shouldn't disturb this.
--
Ticket URL: <https://code.djangoproject.com/ticket/37071#comment:1>
Reply all
Reply to author
Forward
0 new messages