[英]What are best practices for adding initial configuration data to a Python Django instance?
I have a Django application that has a Setting
model. I've manually added the configuration data to the database through the Django Admin UI/Django Console but I want to package up this data and have it automatically created when an individual creates/upgrades an instance of this app.我有一个 Django 应用程序,它的
Setting
为 model。我已经通过 Django 管理 UI/Django 控制台手动将配置数据添加到数据库中,但我想 package 添加此数据并在个人创建/升级时自动创建它这个应用程序的实例。 What are some of the best ways to accomplish this?实现这一目标的最佳方法有哪些?
I have already looked at:我已经看过:
RunSQL
and RunPython
but both only when adding a minimal amount of data.RunSQL
和RunPython
的示例,但两者都仅在添加最少量数据时使用。 Still I haven't found one that seems to line up well with the use case I'm describing.我仍然没有找到一个似乎与我描述的用例一致的。
I also looked at several StackOverflow Q&As on the topic, but all of these are quite old and may not cover any improvements that occurred in the last 8+ years:我还查看了关于该主题的几个 StackOverflow 问答,但所有这些都相当陈旧,可能没有涵盖过去 8 年以上发生的任何改进:
There is a decent amount of settings in this app and the above approaches seem pretty laborious as the amount of data increases.这个应用程序中有相当多的设置,随着数据量的增加,上述方法似乎非常费力。
I have exported the data (to JSON) using fixtures (eg manage.py dumpdata
) but I don't want folks to have to run fixtures to insert data when installing the app.我已经使用固定装置(例如
manage.py dumpdata
)将数据导出(到 JSON),但我不希望人们在安装应用程序时必须运行固定装置来插入数据。
It feels like there should be a really simple way to say, "hey, grab this csv, json, yaml, etc. file and populate the models."感觉应该有一个非常简单的方法来说,“嘿,抓住这个 csv、json、yaml 等文件并填充模型。”
Current Thoughts目前的想法
Barring better recommendations from everyone here my thought is to load the JSON within a data migration and iterate through inserting the data with RunSQL
or RunPython
.除非这里的每个人都提出更好的建议,否则我的想法是在数据迁移中加载 JSON 并通过使用
RunSQL
或RunPython
插入数据进行迭代。 Any other suggestions?还有其他建议吗?
I would not recommend using database (directly) for accessing the settings.我不建议(直接)使用数据库来访问设置。 Because it will make the code messy.
因为它会使代码变得混乱。 For example, for each settings, you have to call the database and you have to query like
MyModel.objects.get(...)
.例如,对于每个设置,您必须调用数据库并且必须像
MyModel.objects.get(...)
一样进行查询。 Even if you write a function to reduce repetitive code, it still won't reduce DB query.即使你写了一个function来减少重复的代码,它仍然不会减少数据库查询。
Hence, I would recommend using libraries like django-constance if your settings are changing dynamically.因此,如果您的设置是动态变化的,我建议使用像django-constance这样的库。 In Django Constance, you can store the default configurations in settings.py (or a separate python file which can be imported in settings.py).
在 Django Constance 中,您可以将默认配置存储在 settings.py 中(或者单独的 python 文件,可以在 settings.py 中导入)。 Like this (copy pasted from documentation):
像这样(从文档中复制粘贴):
INSTALLED_APPS = (
...
'constance',
)
CONSTANCE_CONFIG = {
'BANNER': ("The national cheese emporium", 'Name of the shop '
'The national cheese emporium'),
...
}
The adminsite will look like this:管理站点将如下所示:
And access the variable in code:并访问代码中的变量:
from constance import config
print(config.BANNER)
Then you can modify the value in the admin site based on your need.然后您可以根据需要修改管理站点中的值。 The reason I recommend this way because the setting should be easily accessible from code and you can modify the settings from admin site dynamically.
我之所以推荐这种方式,是因为该设置应该可以从代码轻松访问,并且您可以从管理站点动态修改设置。 Also settings are configurable with custom fields and other features.
还可以使用自定义字段和其他功能配置设置。
If there are pre-existing instances of this application which you want to update with Django-Constance, then I suggest writing a Django Management Command , which will sync the table from existing settings to the Django Constance table.如果您想要使用 Django-Constance 更新此应用程序的预先存在的实例,那么我建议编写一个Django 管理命令,它将表从现有设置同步到 Django Constance 表。
You should use docker to setup your environment你应该使用docker来设置你的环境
Find article on how to do it with Django here .在此处查找有关如何使用 Django 执行此操作的文章。
If need to know about docker and how it works, please go to docker docs .如果需要了解 docker 及其工作原理,请访问 go 至docker 文档。
We can run function at the start of our django app.我们可以在 django 应用程序的开头运行 function。 suppose we have a app named core, in the apps.py file we have
假设我们有一个名为 core 的应用程序,在我们的 apps.py 文件中
from django.apps import AppConfig
class CoreConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'core'
def ready(self):
# your initial work at the start of app
from core.utils import populate_initial_settings
populate_initial_settings()
This will be run every time at the start of django server so you have to handle the logic accordingly.这将在 django 服务器每次启动时运行,因此您必须相应地处理逻辑。 Like first counting the Setting objects if count = 0 populate Setting model otherwise not.
就像首先对设置对象进行计数一样,如果计数 = 0,则填充设置 model,否则不填充。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.