How to Unit Test a Django Model with managed = False

Django
2018-02-05 08:02 (8 years ago)
How to Unit Test a Django Model with managed = False

In Django, when you set Meta: managed = False on a model, you may encounter the following error during unit tests from other apps when trying to perform operations like create():

django.db.utils.ProgrammingError: (1146, "Table 'your_app.your_model' doesn't exist")

This error often occurs when you attempt to test a model created using ./manage.py inspectdb.

In such cases,

Simplifying the Testing of Unmanaged Database Models in Django | Caktus Group
provides a workaround, but

you can also try the following in your your_app/tests/__init__.py:

import sys

if 'test' in sys.argv:
# For testing
from ..models import YourModel

YourModel._meta.managed = True

Writing this in tests/__init__.py should be sufficient.

Currently unrated
The author runs the application development company Cyberneura.
We look forward to discussing your development needs.

Categories

Archive