How to Unit Test a Django Model with managed = False

Django
2018-02-05 17:02 (6 years ago) ytyng

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

Comments

Archive

2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011