Derek Kwok Derek Kwok - 1 year ago 194
Python Question

How to create table during Django tests with managed = False

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 - 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.

Answer Source

Check out this blog post: It describes in detail the creation of a test runner for unmanaged models.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download