django源码分析之项目配置(setting)

项目配置

在项目创建的时候,Django会自动处理大部分工作,但还有一些事情是它没法自动完成的,需要通过项目配置文件完成(一般是文件<projetc>/settings.py),比如数据库的配置,模版的配置,日志的配置,甚至是django app的配置,具体如何配置以及可以配置哪些内容网上有一堆资料,也可以查看官方的文档,本文主要带你了解配置文件是如何生效的以及背后的实现逻辑。

代码分析

ENVIRONMENT_VARIABLE = "DJANGO_SETTINGS_MODULE"

环境变量DJANGO_SETTINGS_MODULE的默认值,它指向当前project的settings文件。

class LazySettings(LazyObject):
    """
    A lazy proxy for either global Django settings or a custom settings object.
    The user can manually configure settings prior to using them. Otherwise,
    Django uses the settings module pointed to by DJANGO_SETTINGS_MODULE.
    """
    def _setup(self, name=None):
        """
        Load the settings module pointed to by the environment variable. This
        is used the first time settings are needed, if the user hasn't
        configured settings manually.
        """
        # 获取 project的settings文件,默认的配置settings对象的方法,就是通过设置DJANGO_SETTINGS_MODULE。还有第二种配置的方法,后面很快就会介绍到。
        settings_module = os.environ.get(ENVIRONMENT_VARIABLE)
        if not settings_module:
            desc = ("setting %s" % name) if name else "settings"
            raise ImproperlyConfigured(
                "Requested %s, but settings are not configured. "
                "You must either define the environment variable %s "
                "or call settings.configure() before accessing settings."
                % (desc, ENVIRONMENT_VARIABLE))
        # 根据settings文件去实例化settings对象
        self._wrapped = Settings(settings_module)

    def __repr__(self):
        # Hardcode the class name as otherwise it yields 'Settings'.
        if self._wrapped is empty:
            return '<LazySettings [Unevaluated]>'
        return '<LazySettings "%(settings_module)s">' % {
            'settings_module': self._wrapped.SETTINGS_MODULE,
        }

    def __getattr__(self, name):
        """Return the value of a setting and cache it in self.__dict__."""
        if self._wrapped is empty:
            self._setup(name)
        # 获取name对应的属性,同时用__dict__来缓存改值
        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':
            # 如果设置的属性是_wrapped(该属性值是setting对象),就把之前缓存的属性都清空,因为setting对象变了,所以之前缓存的属性都没有意义
            self.__dict__.clear()
        else:
            # 其他情况下,只清空对应属性的缓存
            self.__dict__.pop(name, None)
        super().__setattr__(name, value)

    def __delattr__(self, name):
        """Delete a setting and clear it from cache if needed."""
        super().__delattr__(name)
        # 清空name对应属性的缓存
        self.__dict__.pop(name, None)

    # 第二种配置settings对象的方法,通过调用settings.configure()
    def configure(self, default_settings=global_settings, **options):
        """
        Called to manually configure the settings. The 'default_settings'
        parameter sets where to retrieve any unspecified values from (its
        argument must support attribute access (__getattr__)).
        """
        # 如果已经设置settings过,就会报错
        if self._wrapped is not empty:
            raise RuntimeError('Settings already configured.')
        # 默认根据global_settings来实例化UserSettingsHolder,也可以用户自己传入自定义的
        holder = UserSettingsHolder(default_settings)
        # 根据设置的字典属性覆盖配置
        for name, value in options.items():
            setattr(holder, name, value)
        self._wrapped = holder

    @property
    def configured(self):
        """Return True if the settings have already been configured."""
        return self._wrapped is not empty

LazySettings是一个LazyObject,是settings对象的一个有惰性加载功能的代理,包裹了settings对象。惰性加载相关的介绍看上一篇文章。settings对象存在其_wrapped变量中。有两个地方可以设置其_wrapped,对应的就是有两种方法可以让配置生效:

方法1是设置环境变量DJANGO_SETTINGS_MODULE,e.g.:os.environ.setdefault("DJANGO_SETTINGS_MODULE", "demo.settings")

方法2,调用settings.configure(),e.g.:settings.configure(DEBUG=True)

class Settings:
    def __init__(self, settings_module):
        # update this dict from global settings (but only for ALL_CAPS settings)
        # 从global_settings导入Django默认的配置,对应的文件django/conf/global_settings.py
        for setting in dir(global_settings):
            # 只有大写的配置会被导入生效
            if setting.isupper():
                setattr(self, setting, getattr(global_settings, setting))

        # store the settings module in case someone later cares
        self.SETTINGS_MODULE = settings_module

        # 导入用户设置的配置文件模块
        mod = importlib.import_module(self.SETTINGS_MODULE)

        # 对应值是元组的配置项
        tuple_settings = (
            "INSTALLED_APPS",
            "TEMPLATE_DIRS",
            "LOCALE_PATHS",
        )
        # 记录明确设置过的配置项
        self._explicit_settings = set()
        # 便利用户的配置
        for setting in dir(mod):
            # 同样只有大写的配置会生效
            if setting.isupper():
                # 获取配置项对应的值
                setting_value = getattr(mod, setting)
                # 判断值应该是元组的配置项是否符合要求
                if (setting in tuple_settings and
                        not isinstance(setting_value, (list, tuple))):
                    raise ImproperlyConfigured("The %s setting must be a list or a tuple. " % setting)
                # 生效对应的配置,同时记录该配置项
                setattr(self, setting, setting_value)
                self._explicit_settings.add(setting)
        # SECRET_KEY相关的检查,必须配置
        if not self.SECRET_KEY:
            raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")

        if self.is_overridden('DEFAULT_CONTENT_TYPE'):
            warnings.warn('The DEFAULT_CONTENT_TYPE setting is deprecated.', RemovedInDjango30Warning)
        # time zone相关的
        if hasattr(time, 'tzset') and self.TIME_ZONE:
            # When we can, attempt to validate the timezone. If we can't find
            # this file, no check happens and it's harmless.
            zoneinfo_root = '/usr/share/zoneinfo'
            if (os.path.exists(zoneinfo_root) and not
                    os.path.exists(os.path.join(zoneinfo_root, *(self.TIME_ZONE.split('/'))))):
                raise ValueError("Incorrect timezone setting: %s" % self.TIME_ZONE)
            # Move the time zone info into os.environ. See ticket #2315 for why
            # we don't do this unconditionally (breaks Windows).
            os.environ['TZ'] = self.TIME_ZONE
            time.tzset()

    def is_overridden(self, setting):
        return setting in self._explicit_settings

    def __repr__(self):
        return '<%(cls)s "%(settings_module)s">' % {
            'cls': self.__class__.__name__,
            'settings_module': self.SETTINGS_MODULE,
        }

Settings类实现了Django配置的载入功能,它先从django/conf/global_settings.py导入默认的配置,然后再从DJANGO_SETTINGS_MODULE指向的配置文件中导入自定义的配置。其中的_explicit_settings用来记录我们自定义的配置项,同时被is_overridden用来判断某个配置项是否被我们自定义的覆盖了。

class UserSettingsHolder:
    """Holder for user configured settings."""
    # SETTINGS_MODULE doesn't make much sense in the manually configured
    # (standalone) case.
    SETTINGS_MODULE = None

    def __init__(self, default_settings):
        """
        Requests for configuration variables not in this class are satisfied
        from the module specified in default_settings (if possible).
        """
        # 用来记录被删除的配置项
        self.__dict__['_deleted'] = set()
        # 默认的配置settings对象
        self.default_settings = default_settings

    # 获取配置,先判断属性是否已经被删除
    def __getattr__(self, name):
        if name in self._deleted:
            raise AttributeError
        return getattr(self.default_settings, name)

    # 设置配置,如果之前被删了,先从删除记录里面清除,然后加上该配置
    def __setattr__(self, name, value):
        self._deleted.discard(name)
        if name == 'DEFAULT_CONTENT_TYPE':
            warnings.warn('The DEFAULT_CONTENT_TYPE setting is deprecated.', RemovedInDjango30Warning)
        super().__setattr__(name, value)

    def __delattr__(self, name):
        self._deleted.add(name)
        if hasattr(self, name):
            super().__delattr__(name)

    def __dir__(self):
        return sorted(
            s for s in list(self.__dict__) + dir(self.default_settings)
            if s not in self._deleted
        )

    def is_overridden(self, setting):
        deleted = (setting in self._deleted)
        set_locally = (setting in self.__dict__)
        set_on_default = getattr(self.default_settings, 'is_overridden', lambda s: False)(setting)
        return deleted or set_locally or set_on_default

    def __repr__(self):
        return '<%(cls)s>' % {
            'cls': self.__class__.__name__,
        }

UserSettingsHolder类和其实和Settings类差不多,也是实现了Django配置的加载功能。区别是加载配置的方法不同,它是根据传入的default_settings来加载,但是相同的地方是default_settings的默认值也是django/conf/global_settings.py对应的类。同时根据用户传入的自定义的配置来存储项目的配置。

settings = LazySettings()

settings是一个惰性对象,只有实际使用才会触发配置文件的加载。

总结

目前位置基本了解了项目配置生效的实现逻辑。可以看到如果要让配置生效,我们必须做下面两件事情之中的某一件,要不就是调用configure(),要不就是设置DJANGO_SETTINGS_MODULE环境变量。如果这两件事情都没做,那么配置就不会生效,会报ImportError的异常;但是如果两件事情都做了,系统也会报错,发生RuntimeError的错误。所以这两事情必须做了一件,而且只能做一件。

还有需要说明的是,配置文件不需要去设置所有项目,因为Django已经默认设置好了,默认的设置在django/conf/global_settings.py文件中,我们只需要修改我们使用的配置项就好了。django在编译时,会先载入global_settings.py中的配置,然后加载指定的settings文件,重设我们自定义的配置项。

最后还需要额外强调的是,目前位置只是配置项目文件,有了settings对象而已,如果想使用Django的功能,比如使用ORM,我们还需要做一件事情,需要调用django.setup()来加载配置文件里面的内容,来填充Django的应用程序注册表。如下面的样例:

import django
from django.conf import settings
from myapp import myapp_defaults

settings.configure(default_settings=myapp_defaults, DEBUG=True)
django.setup()

# Now this script or any imported module can use any part of Django it needs.
from myapp import models

推荐阅读更多精彩内容