当我的 Django 应用程序被定义为不允许在路由器中进行迁移时,为什么它会尝试创建迁移?

Why does my Django app attempt to create migrations, when it is defined to not allow migrations in a router?

我有一个名为 dataapi 的 Django 应用程序,它是通过内省 PostgreSQL 数据库编写各种模型文件(每个模式一个)构建的。它可以工作,并通过 ORM 为我们提供对所需数据的只读访问,但我按模式将模型分解为多个文件,而不是将它们全部放在一个(巨大的)models.py 文件中。

我已经定义了一个 ROOT/config/routers.py 文件告诉 Django 我不希望这个应用程序的模型使用迁移:

class DataAPIRouter(object):
    """
    A router to control all database operations on models in the
    dataapi application.
    """

    def db_for_read(self, model, **hints):
        """
        Attempts to read dataapi models go to mssqlwrds.
        """

        if model._meta.app_label == 'dataapi':
            return 'pgdata'
        return None


    def db_for_write(self, model, **hints):
        """
        Attempts to write dataapi models go to mssqlwrds.
        """

        if model._meta.app_label == 'dataapi':
            return 'pgdata'
        return None


    def allow_relation(self, obj1, obj2, **hints):
        """
        Allow relations if a model in the dataapi app is involved.
        """

        if obj1._meta.app_label == 'dataapi' or obj2._meta.app_label == 'dataapi':
            return True
        return None


    def allow_migrate(self, db, app_label, model_name=None, **hints):
        """
        Make sure the dataapi app doesn't use migrations.
        """

        if app_label == 'dataapi':
            return False
        return True

在我的设置中,我定义了路由器:

DATABASE_ROUTERS = [
    'config.routers.DataAPIRouter',
]

然而,当我 运行 使用 makemigrations 干 运行 时,它仍然显示它正在访问应用程序:

(project) [vagrant@vagrant project]$ ./manage.py makemigrations --dry-run
Migrations for 'dataapi':
  0001_initial.py:
    - Create model aco_amda
    - Create model aco_imda
    - Create model aco_indfnta
    - Create model aco_indfntq
    [...]

我错过了什么吗?我怎样才能让这个应用程序被迁移忽略?

事实证明,这是设计使然。正如@knbk 指出的那样,路由器中的此设置只会在您执行 python manage.py migrate 时阻止迁移发生。当您 运行 python manage.py makemigrations 时,它仍会 创建 迁移。我已经向 Django 发布了 PR 以在已被接受的文档中澄清这一点。