如何使用managed = False在Django测试期间创建表 [英] How to create table during Django tests with managed = False

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

问题描述

我有一个model = False的模型。

  class SampleModel(models.Model):
apple = models.CharField(max_length = 30)
orange = models.CharField(max_length = 30)

class Meta:
managed = False

我有一个单元测试创​​建一个SampleModel,但是当我运行测试我得到:

  DatabaseError:没有这样的表:SAMPLE_SAMPLE_MODEL 

django文档 - https://docs.djangoproject.com/en/dev/ref/models/options/#managed 记录以下内容:


对于涉及managed = False的模型的测试,由
确保正确的表创建作为测试设置的一部分。


如何在测试设置过程中实际创建表?或者,如何让我这样做,当我运行测试时,这个模型在测试期间有managed = True?



在实际的应用程序,该模型实际上由数据库中的视图支持。但是对于测试期间,我想把它当作表格,并且可以在其中插入测试数据。

解决方案

p>查看这篇博文: http://www.caktusgroup.com/blog/2010/09/24/simplifying-the-testing-of-unmanaged-database-models-in-django/ 详细描述了测试的创建非管理型号的跑步者。


I have a model with managed = False.

class SampleModel(models.Model):
    apple = models.CharField(max_length=30)
    orange = models.CharField(max_length=30)

    class Meta:
        managed = False

I have a unit test which creates a SampleModel, however when I run the test I get:

DatabaseError: no such table: SAMPLE_SAMPLE_MODEL

The django docs - https://docs.djangoproject.com/en/dev/ref/models/options/#managed documents the following:

For tests involving models with managed=False, it's up to you to ensure the correct tables are created as part of the test setup.

How can I actually "create" the tables during the test setup? Or alternatively, how can I make it so that when I am running tests, this model has "managed = True" for the duration of the test?

In the real application, this model is actually backed by a view in the database. However for the during of the test, I would like to treat this as a table and be able to insert test data in there.

解决方案

Check out this blog post: http://www.caktusgroup.com/blog/2010/09/24/simplifying-the-testing-of-unmanaged-database-models-in-django/ It describes in detail the creation of a test runner for unmanaged models.

这篇关于如何使用managed = False在Django测试期间创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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