Why is django's settings object a LazyObject?
Check out this section of the Django coding style. The reason is explained in there (quoted below).
In addition to performance, third-party modules can modify settings when they are imported. Accessing settings should be delayed to ensure this configuration happens first.
Modules should not in general use settings stored in django.conf.settings at the top level (i.e. evaluated when the module is imported). The explanation for this is as follows:
Manual configuration of settings (i.e. not relying on the DJANGO_SETTINGS_MODULE environment variable) is allowed and possible as follows:
from django.conf import settings settings.configure({}, SOME_SETTING='foo')
However, if any setting is accessed before the settings.configure line, this will not work. (Internally, settings is a
LazyObject
which configures itself automatically when the settings are accessed if it has not already been configured).So, if there is a module containing some code as follows:
from django.conf import settings from django.core.urlresolvers import get_callable default_foo_view = get_callable(settings.FOO_EXAMPLE_VIEW)
...then importing this module will cause the settings object to be configured. That means that the ability for third parties to import the module at the top level is incompatible with the ability to configure the settings object manually, or makes it very difficult in some circumstances.
Instead of the above code, a level of laziness or indirection must be used, such as
django.utils.functional.LazyObject
,django.utils.functional.lazy()
orlambda
.
Its a proxy object that abstracts the actual settings files, and makes it light weight until you actually access the settings you want. Once you start accessing the attributes, it will then load on demand. The idea is to reduce overhead in loading settings until you need them.