未创建Django测试表 [英] Django test tables are not being created

查看:71
本文介绍了未创建Django测试表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为django项目编写测试用例,但是当我运行时 "$ ./manage.py test"命令 它正在创建测试数据库,但未创建任何表,并且我收到一个错误消息,表明该表不存在.欢迎任何建议.这是我通过"./manage.py inspectdb> models.py"

I'm trying to write test cases for my django project but when I run "$ ./manage.py test" command its creating test database but its not creating any tables and I'm getting an error that table does't exists. Any suggestions are welcome. Here is my model which i have created through "./manage.py inspectdb > models.py"

class MyCustomModel(models.Model):
    name = models.CharField(max_length=200)
    last_name = models.CharField(max_length=200)

    class Meta:
       managed = False
       db_table = 'MY_TABLE'

推荐答案

pytest在测试过程中为非托管模型创建表

添加 --nomigrations 到您的pytest.ini

[pytest] addopts = --nomigrations

[pytest] addopts = --nomigrations

并将其添加到您的顶级conftest.py

and add this to your top-level conftest.py

@pytest.fixture(autouser=True, scope="session")
def django_test_environment(django_test_environment):
    from django.apps import apps

    get_models = apps.get_models

    for m in [m for m in get_models() if not m._meta.managed]:
        m._meta.managed = True

它会神奇

这篇关于未创建Django测试表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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