别再用`import settings`导入django settings了

起因

  • 记得之前刚开始写django项目时,用import settings导入settings,被老大叼了。当时也没怎么想,只是照做了。直到最近参与一个同事写的django项目中,发现了好的地方都是使用这种方式导入settings。这个其中的影响,不能说难受,只能说贼难受。。。为了成功说服他改掉这种写法,那我得先搞懂来不是,所以有了这篇文章。

对比

from django.conf import settings与import settings的区别。前者是从django模版导入settings,是django官方推荐的导入settings的方式,后者是从项目模块中导入settings。

  • "import settings"导入将在 sys.path 中找到的第一个名为 settings.py 的python模块。它只允许访问你定义的设置文件,所以,如果你试图访问一个没有在你的设置文件中指定,django又具备的默认设置( django.conf.global_settings ),你会得到一个错误。
  • django.conf.settings 不是一个文件,而是一个懒加载对象。当你使用 from django.conf import settings时,Django还会做其他检查。

影响

非预期的变量值

场景一

  • 本地开发时,经常需要使用manage.py在终端启动各种命令。指定非settings文件运行(如:settings_local.py),加载的仍然是settings.py中的配置。

    ini 复制代码
    python manage.py runserver --settings=proj.settings_local
    python manage.py migrate --settings=proj.settings_local
    python manage.py collectstatic --settings=proj.settings_local
  • 怎么影响到的

    • settings_local.py中全力引用settings.py的变量,然后对一些变量进行覆盖
lua 复制代码
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings_local')

场景二

  • 使用环境变量DJANGO_SETTINGS_MODULE指定settings时

源码分析

  • Django启动的时候,会使用setup方法,源码如下。因此每次启动时会执行" from django.conf import settings"代码,settings中配置加载到内存中。
python 复制代码
# django/__init__.py
def setup(set_prefix=True):
    """
Configure the settings (this happens as a side effect of accessing the
first setting), configure logging and populate the app registry.
Set the thread-local urlresolvers script prefix if `set_prefix` is True.
"""
from django.apps import apps
    from django.conf import settings
    from django.urls import set_script_prefix
    from django.utils.log import configure_logging

    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
    if set_prefix:
        set_script_prefix(
            '/' if settings.FORCE_SCRIPT_NAME is None else settings.FORCE_SCRIPT_NAME
        )
    apps.populate(settings.INSTALLED_APPS)
  • 使用wsgi启动时,也是间接调用django的setup方法。
python 复制代码
# django/core/wsgi.py 文件
def get_wsgi_application():
    """
The public interface to Django's WSGI support. Return a WSGI callable.

Avoids making django.core.handlers.WSGIHandler a public API, in case the
internal WSGI implementation changes or moves in the future.
"""
django.setup(set_prefix=False)
    return WSGIHandler()
  • 使用在manage.py在终端启动时,调用的是ManagementUtility对象的execute方法,其中有调用setup()方法
python 复制代码
# django/core/management/__init__.py
def execute_from_command_line(argv=None):
    """Run a ManagementUtility."""
    utility = ManagementUtility(argv)
    utility.execute()

循环引用

  • 如果在settings中引用其他模块时,又以项目文件方式导入settings,可能会导致循环引用。

懒加载

Django的懒加载(lazy loading)机制指的是在需要时才加载某些设置,而不是在应用程序启动时就立即加载所有设置。这种机制有几个重要的意义:

  1. 性能优化: Django的settings.py可能包含大量的配置项,但并不是所有配置项都会在应用程序启动时立即使用。通过懒加载,只有在需要时才加载相应的配置项,可以节省内存和启动时间,提高应用程序的性能。
  2. 如果不设置成懒加载,在顶层导入模块的能力与手动配置设置对象的能力不兼容

因此,如果直接在项目中导入settings文件,则不会具备这样的优点。

总结

这篇文章起源于对Django项目中导入settings方式的疑惑。对比了from django.conf import settings与import settings的区别,后者从项目模块中导入settings,可能导致非预期的变量值。源码分析表明,Django启动时会使用内置模块加载settings配置,若项目中直接导入settings则无法享受到懒加载带来的性能优势。同时可能导致循环引用问题,非预期的变量值,甚至是报错。

引用

相关推荐
软件技术NINI5 分钟前
基于 SpringBoot 的共享植物管理系统
java·spring boot·后端
FYKJ_201025 分钟前
springboot网上汽车租赁系统93523-计算机毕业设计、课程设计
java·vue.js·spring boot·后端·python·汽车·课程设计
计算机毕业编程指导师37 分钟前
计算机毕设选题:马术俱乐部管理系统 SpringBoot 全解析(功能模块 + 数据表设计 + 核心代码)
java·spring boot·后端·mysql·elementui·毕业设计·马术俱乐部
码事漫谈9 小时前
WorkBuddy 固然很香,但豆姐她免费呀
后端
金銀銅鐵10 小时前
带您看懂字节码指令(八):数组里有什么
java·jvm·后端
zark_chuan10 小时前
怎么让 AI 写十万行代码不腐化:我的 AGENTS.md、Pre-Push 门禁与活体 Wiki
后端
Rain的Java大神之路12 小时前
Spring Boot 3 升级实战:真正吃掉工期的,是 MyBatis-Plus 坐标和 Security 6 重写
java·后端·面试
源码_空间12 小时前
基于Springboot在线考试系统(源码+文档)
java·spring boot·后端
geovindu13 小时前
python: PCA
开发语言·后端·python·算法
冰美式只要糖16 小时前
Prefill 长短请求混批,为什么 128 Token 也要等 2.58 秒?
后端