gpt4 book ai didi

python - 自动加载 Django Fixture

转载 作者:行者123 更新时间:2023-12-04 23:00:10 25 4
gpt4 key购买 nike

我正在运行 Django 1.7。我的项目文件树是这样的:

/project/app/fixtures/initial_data.json
/project/app/settings.py

我知道我可以运行 python manage.py loaddata app/fixtures/initial_data.json将用于填充我的数据库的命令,但我想在 python manage.py migrate 时自动加载它正在运行。我的设置包括:
FIXTURE_DIRS = (
os.path.join(BASE_DIR, '/app/fixtures/'),
)

但是在运行 migrate 时不应用夹具。似乎是什么问题?

最佳答案

恐怕不是,这不是您的问题,因为自 Django 1.7 以来已弃用:

READ HERE

Automatically loading initial data fixtures¶

Deprecated since version 1.7: If an application uses migrations, there is no automatic loading of fixtures. Since migrations will be required for applications in Django 1.9, this behavior is considered deprecated. If you want to load initial data for an app, consider doing it in a data migration.

If you create a fixture named initial_data.[xml/yaml/json], that fixture will be loaded every time you run migrate. This is extremely convenient, but be careful: remember that the data will be refreshed every time you run migrate. So don’t use initial_data for data you’ll want to edit.



如果您真的希望它起作用,您可以随时自定义您的 manage.py ,
# import execute_from_command_line
from django.core.management import execute_from_command_line

# add these lines for loading data
if len(sys.argv) == 2 and sys.argv[1] == 'migrate':
execute_from_command_line(['manage.py', 'loaddata'])

execute_from_command_line(sys.argv)

希望这可以帮助。

关于python - 自动加载 Django Fixture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27808486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com