我想執行syncdb後添加一些Python代碼,所以我決定寫一個build.py
該做的一切,包括執行syncdb。Django的call_command執行syncdb失敗:無法打開數據庫文件
我寫的東西build.py
爲:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
from django.core.management import call_command
from django.contrib.auth.models import User
call_command('syncdb', interactive=False)
但是當我運行build.py
,它說:
Traceback (most recent call last):
File "/home/csimstu/PycharmProjects/TeenHope/TeenHope/build.py", line 5, in <module>
call_command('syncdb', interactive=False)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 161, in call_command
return klass.execute(*args, **defaults)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 255, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 385, in handle
return self.handle_noargs(**options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/syncdb.py", line 56, in handle_noargs
cursor = connection.cursor()
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/__init__.py", line 324, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py", line 306, in _cursor
self._sqlite_create_connection()
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py", line 296, in _sqlite_create_connection
self.connection = Database.connect(**kwargs)
sqlite3.OperationalError: unable to open database file
我試過./manage.py執行syncdb和交互shell使用call_command模式,並且兩種方式都完美無缺。怎麼會這樣?
可能的重複:http://stackoverflow.com/questions/4636970/sqlite3-operationalerror-unable-to-open-database-file –
不,不可能。它在交互式shell中工作正常。也許我只需要弄清楚./manage.py shell命令的輸入。 –
或者給文件夾的所有權限也許可以。 –