如何为可重用的Django应用程序进行迁移?

时间:2021-09-07 18:14:31

I am making a reusable Django app without a project. This is the directory structure:

我正在制作一个没有项目的可重用的Django应用程序。这是目录结构:

/
/myapp/
/myapp/models.py
/myapp/migrations/
/myapp/migrations/__init__.py

When I run django-admin makemigrations I get the following error:

当我运行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.

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?

显然,这是因为我没有配置设置模块,因为这是一个可重用的应用程序。但是,我仍然希望通过我的应用程序发送迁移。我该怎么做?

2 个解决方案

#1


10  

You need a functional Django project (with your app installed in it) to make migrations.

您需要一个功能性的Django项目(安装了您的应用程序)来进行迁移。

A common way to do this is to have a "test" project which contains the bare necessities of a Django project, that you can run to make migrations etc. The migrations will be created in the right place inside your app directory so you can still have proper version control etc within your own reusable app.

执行此操作的常用方法是创建一个“测试”项目,其中包含Django项目的基本必需项,您可以运行以进行迁移等。将在应用程序目录中的正确位置创建迁移,以便您仍然可以在您自己的可重用应用程序中有适当的版本控制等。

The migrations created in this way will be self-contained (assuming your models don't depend on models from other apps) and can be shipped as part of your packaged, reusable app.

以这种方式创建的迁移将是自包含的(假设您的模型不依赖于其他应用程序的模型),并且可以作为打包的可重用应用程序的一部分提供。

Many of the larger Django-based projects actually ship a test project as part of their code, so that developers can quickly get it running in order to test apps and make migrations etc.

许多较大的基于Django的项目实际上将测试项目作为其代码的一部分,因此开发人员可以快速运行它以测试应用程序并进行迁移等。

#2


7  

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)

and you should run it by python make_migrations.py. Hope it helps someone!

你应该通过python make_migrations.py来运行它。希望它可以帮到某人!

#1


10  

You need a functional Django project (with your app installed in it) to make migrations.

您需要一个功能性的Django项目(安装了您的应用程序)来进行迁移。

A common way to do this is to have a "test" project which contains the bare necessities of a Django project, that you can run to make migrations etc. The migrations will be created in the right place inside your app directory so you can still have proper version control etc within your own reusable app.

执行此操作的常用方法是创建一个“测试”项目,其中包含Django项目的基本必需项,您可以运行以进行迁移等。将在应用程序目录中的正确位置创建迁移,以便您仍然可以在您自己的可重用应用程序中有适当的版本控制等。

The migrations created in this way will be self-contained (assuming your models don't depend on models from other apps) and can be shipped as part of your packaged, reusable app.

以这种方式创建的迁移将是自包含的(假设您的模型不依赖于其他应用程序的模型),并且可以作为打包的可重用应用程序的一部分提供。

Many of the larger Django-based projects actually ship a test project as part of their code, so that developers can quickly get it running in order to test apps and make migrations etc.

许多较大的基于Django的项目实际上将测试项目作为其代码的一部分,因此开发人员可以快速运行它以测试应用程序并进行迁移等。

#2


7  

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)

and you should run it by python make_migrations.py. Hope it helps someone!

你应该通过python make_migrations.py来运行它。希望它可以帮到某人!