【问题标题】:ValueError: No module named 'notmigrations' during unit testsValueError:单元测试期间没有名为“notmigrations”的模块
【发布时间】:2019-09-17 13:45:39
【问题描述】:

我有 django 应用程序 2.1.7 和 django-tenant 插件(在数据库中为 saas 创建模式)。

我的问题是单元测试。我运行命令: python manage.py test --settings=project.settings_test 我收到一个错误:ImportError: No module named 'notmigrations'

我在 settings_test 文件中的代码

from .settings_base import *


class DisableMigrations(object):
    def __contains__(self, item):
        return True

    def __getitem__(self, item):
        return 'notmigrations'

MIGRATION_MODULES = DisableMigrations()

【问题讨论】:

    标签: python django unit-testing multi-tenant django-unittest


    【解决方案1】:

    您正在使用一个古老的 hack,用于真正旧版本的 Django (

    如果您想在测试中禁用迁移,请使用the modern approach, which is putting the value to None in MIGRATION_MODULES setting

    当您提供 None 作为应用程序的值时,无论现有的迁移子模块如何,Django 都会将该应用程序视为没有迁移的应用程序。例如,这可以在测试设置文件中用于在测试时跳过迁移(仍会为应用的模型创建表)。

    # test_settings.py
    from settings import *
    
    MIGRATION_MODULES = {
        'auth': None,
        'contenttypes': None,
        'sessions': None,
        ...
        'myapp1': None,
        'myapp2': None,
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-04
      • 1970-01-01
      • 1970-01-01
      • 2018-04-26
      • 2021-06-10
      • 1970-01-01
      • 1970-01-01
      • 2013-12-01
      相关资源
      最近更新 更多