如何为可重复使用的Django应用进行迁移? [英] How to make migrations for a reusable Django app?

查看:71
本文介绍了如何为可重复使用的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?

推荐答案

您需要一个功能正常的Django项目(已安装您的应用程序)进行迁移.

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.

以这种方式创建的迁移将是独立的(假设您的模型不依赖于其他应用程序的模型),并且可以作为打包的可重用应用程序的一部分进行运输.

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.

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

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应用进行迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆