How to make migrations for a reusable Django app?(如何为可重用的 Django 应用程序进行迁移?)
问题描述
我正在制作一个没有项目的可重用 Django 应用程序.这是目录结构:
I am making a reusable Django app without a project. This is the directory structure:
/
/myapp/
/myapp/models.py
/myapp/migrations/
/myapp/migrations/__init__.py
当我运行 django-admin makemigrations 时出现以下错误:
When I run django-admin makemigrations I get the following error:
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.
显然,这是因为我没有配置设置模块,因为这是一个可重复使用的应用程序.但是,我仍然想使用我的应用程序进行迁移.我怎样才能制作它们?
Obviously, this is because I don't have a settings module configured, because this is a reusable app. However, I would still like to ship migrations with my app. How can I make them?
推荐答案
其实你不需要有项目,你只需要设置文件和脚本,运行迁移创建.设置必须包含以下内容(最少):
Actually, you don't need to have project, all you need is settings file and script, that runs migrations creation. Settings must contain folowing (minimum):
# test_settings.py
DEBUG = True
SECRET_KEY = 'fake-key'
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'your_app'
]
进行迁移的脚本应该如下所示:
And the script, that makes migrations should look like this:
# 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 运行它.希望它可以帮助某人!
and you should run it by python make_migrations.py. Hope it helps someone!
这篇关于如何为可重用的 Django 应用程序进行迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何为可重用的 Django 应用程序进行迁移?
基础教程推荐
- matplotlib 设置 yaxis 标签大小 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
