如何为可重用的Django应用进行迁移?
我正在制作没有项目的可重用Django应用。这是目录结构:
/
/myapp/
/myapp/models.py
/myapp/migrations/
/myapp/migrations/__init__.py
当我运行时django-admin makemigrations
,出现以下错误:
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
显然,这是因为我没有配置设置模块,因为这是可重用的应用程序。但是,我仍然希望随应用程序一起迁移。我该怎么做?
-
实际上,您不需要项目,只需要设置文件和脚本即可运行迁移创建。设置必须包含以下内容(最低要求):
# test_settings.py DEBUG = True SECRET_KEY = 'fake-key' INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'your_app' ]
而使迁移的脚本应如下所示:
# make_migrations.py import os import sys if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_settings") from django.core.management import execute_from_command_line args = sys.argv + ["makemigrations", "your_app"] execute_from_command_line(args)
并且您应该通过运行它
python make_migrations.py
。希望它能对某人有所帮助!