gpt4 book ai didi

python - 通过python脚本加载fixture.json

转载 作者:行者123 更新时间:2023-11-30 23:09:40 25 4
gpt4 key购买 nike

我有 Django 模型的固定 JSON 文件,我想通过 python 脚本更新数据。我不想在终端 python manage.py loaddata Fixture.json 上运行命令。我想通过 python 脚本来做到这一点。

我已经为 Django 创建了虚拟环境。我还想通过脚本启动虚拟环境。我写了一个小脚本,但它不起作用。

from subprocess import call
from os import chdir, getcwd

original_directory = getcwd()
chdir('/home/naresh/test_django')
call('source bin/activate', shell = True)
chdir('graphossss/')
call('python manage.py loaddata background/fixtures.json', shell= True)
chdir(original_directory)

出现错误:

"File "manage.py", line 8, in

from django.core.management import execute_from_command_line

ImportError: No module named django.core.management"

这意味着虚拟环境未激活...

最佳答案

我通过调用call_command方法解决了这个问题

from django.core.management import call_command
call_command('loaddata', 'data.json', stdout=out, verbosity=0)

关于python - 通过python脚本加载fixture.json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31030268/

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