【发布时间】:2020-04-16 18:04:47
【问题描述】:
我正在尝试在启动 Django 后从固定装置填充模型。
我发现,可以在AppConfig 的ready() 方法中调用自定义方法。
所以,当我尝试下一步时:
class ApplicationConfig(AppConfig):
name = 'application'
def ready(self):
from django.core.management import call_command
call_command('loaddata', 'application/fixtures/some_info.json')
我收到错误消息:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/app/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/app/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 357, in execute
django.setup()
File "/app/venv/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/app/venv/lib/python3.6/site-packages/django/apps/registry.py", line 83, in populate
raise RuntimeError("populate() isn't reentrant")
RuntimeError: populate() isn\'t reentrant
还有其他方法可以在启动时预填充模型吗?或者,如何解决我的问题?
【问题讨论】:
-
我不会,因为唯一有用的情况是在运行测试时,并且已经在那里实现了。您的数据存储是否如此短暂以至于您有这种担忧?
标签: django model initialization