Django:在project.settings.py文件中使用光标运行SQL查询

发布于 2025-01-31 22:36:59 字数 938 浏览 1 评论 0原文

在我的项目中,我通过通过环境文件将其公开给用户,在“ settings.py”文件中制作了一些设置字段。因此,用户可以修改.ENV文件上的值,然后将其用于更新主project settings.py文件中的设置字段。 我想通过将其中一些值迁移到数据库来改进这一点,以便用户可以通过产品的UI进行交互式设置其值,而不必修改.ENV。我采用了以下方法:

  • 在数据库词典中声明默认数据库后,我不知道connection.cursor()运行从数据库中检索设置的RAW SQL查询,如所述在 document
  • 我操纵光标的结果来构建一个词典,其中键正在设置标识符,并且值是用户设置的数据库中的相关值。
  • 然后,该字典用于为每个DJANGO设置变量分配适当的值(即session_cookie_ageMedia_root等)。因此,在每个设置变量而不是执行getenv,我使用相关键从字典中检索值。

我已经在设置中观察了代码的行为。问题在于,当这些设置变量通过django.conf.settings或通过直接导入(从project.setting import import interigt )访问这些设置变量时,它们的值是空字符串,好像首先没有声明。

我已经注意到,在光标实例化之前声明的设置(无论其价值是硬编码还是从.env中检索)都可以正常工作。光标之后的设置似乎无法在settings.py文件之外维护其状态。

谁能让我启发我为什么在设置中使用光标。

In my project, I have made some setting fields within the "settings.py" file configurable by exposing them to the user via an environment file. So the user can modify the values on the .env file, and that is then used to update the setting fields within the main project settings.py file.
I want to improve this by migrating some of this values to the database, so users can set their values interactively via the product's UI instead of having to modify the .env. I have taken the following approach:

  • After the default database has been declared in the DATABASES dictionary, I isntantiate a connection.cursor() to run a raw SQL query that retrieves the settings from the database, as described in the documentation.
  • I manipulate the results of the cursor to construct a dictionary in which keys are setting identifiers and values are the relevant values from the database, as set by the user.
  • This dictionary is then used to assign the appropriate value to each Django setting variable (i.e. SESSION_COOKIE_AGE, MEDIA_ROOT, etc.). So at each setting variable instead of doing a getenv, I retrieve the value from the dictionary using the relevant key.

I have observed the code's behavior within settings.py, and I can see that each setting value gets assigned to the correct variable, identically to how it was when using the previous .env approach. The problem is that when these setting variables are accessed in the code via django.conf.settings or by direct import (from project.settings import SETTING), their value is an empty string, as if it has not been declared in the first place.

I have noticed that settings declared before the cursor is instantiated (regardless of whether their value was hardcoded or retrieved from the .env) work fine. Settings after the cursor seem to not maintain their state outside the settings.py file.

Can anyone please enlighten me as to why using a cursor within settings.py essentially invalidates all setting fields declared after it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月下凄凉 2025-02-07 22:36:59

我设法解决了这个问题。问题是我正在使用django的连接模型(来自django.db导入连接),该连接在默认数据库上执行SQL查询。由于这是在设置中运行的。

解决方案:我不使用django.db.connection(django模型)来执行查询,而是使用Python的pyodbc库,这是项目中使用的数据库引擎。使用pyodbc我能够建立与数据库的连接并运行我的查询(如所述 emer )无论django的状态如何。

I managed to resolve the issue. The problem was that I was using Django's connection model (from django.db import connection), which performs the SQL query on the default database. Since this was running within settings.py while the project was still in its set-up process, it caused the strange behavior described above.

Solution: Instead of using django.db.connection (a Django model) to perform the query, I used Python's pyodbc library instead, which is the database engine used in the project. With pyodbc I was able to establish a connection to the database and run my query (as described here) regardless of Django's state.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文