python - Django:在迁移运行时更改设置值
问题描述
我试图settings.py
在创建迁移时在运行时更改值。
设置.py:
...
magicVar = "initValue"
0002_init:
...
def change_magicVar(apps, schema_editor):
settings.magicVar = "newValue"
...
operations = [
migrations.RunPython(change_magicVar),
]
...
0003_changed_migrations:
...
def print_magicVar(apps, schema_editor):
# Yay! It prints the correct value
print (settings.magicVar) # prints: newValue
...
operations = [
migrations.RunPython(print_magicVar),
migrations.CreateModel(
name='MagicModel',
fields=[
('someMagic',
models.ForeignKey(
# Oops! This still has the old value, i.e *initValue*
# I want to achieve the *newValue* here!
default=settings.magicVar,
...
)),
我想要迁移中的更改值,但看起来该值已被缓存。django 是否提供了一种抽象来刷新迁移缓存并在其中添加新值?如果不是,我有哪些可能的选择才能在默认值中实现该值?
注意:我试图避免这种解决方案,因为我的数据库可能会提供数百万条记录,并且对它们进行迭代并不理想。
由于外部原因,我也试图避免使用django-livesettings
谢谢!
解决方案
你不能以这种方式实现它。您可以查看https://github.com/jazzband/django-constance。
推荐阅读
- java - 我在网上找了很久。但是没有用。请帮助或尝试提供一些想法如何实现这一目标。你能帮我解决这个错误吗?
- excel - Matlab矩阵:获取每行值的标题字符串的单元格数组
- sql - Hive 中的计算列
- javascript - JavaScript 使用 HTML 标签
- docker - Docker:如何在数据库映像中保留入口点
- django - 无法从 django 模型中选择一行以将它们输出到第三个模型
- python - 如何在新列(熊猫)中统一值和列名?
- mysql - 仅使用 ibdata1 恢复 wordpress 数据库
- angular - 同时多次调用 powerbi.embed 会产生错误
- c# - 在组合框中显示表格的信息