---
slug: "django-unmanaged-model-unit-test"
title: "How to Unit Test a Django Model with managed = False"
description: "When using Django, setting `Meta: managed = False` in a model can sometimes lead to a `ProgrammingError` during unit tests from other apps when attempting operations such as `create()`. This is a memo on how to address this issue."
url: "https://www.ytyng.com/en/blog/django-unmanaged-model-unit-test"
publish_date: "2018-02-05T08:02:06Z"
created: "2018-02-05T08:02:06Z"
updated: "2026-02-27T11:57:17.076Z"
categories: ["Django"]
keywords: ""
featured_image_url: "https://media.ytyng.com/resize/20230812/6a1df632407e4386a0cc35d55ac4add3.png.webp?width=768"
has_video: false
has_music: false
video_urls: []
music_urls: []
lang: "en"
---

# How to Unit Test a Django Model with managed = False

<p>In Django, when you set <code>Meta: managed = False</code> on a model, you may encounter the following error during unit tests from other apps when trying to perform operations like <code>create()</code>:</p>
<pre>django.db.utils.ProgrammingError: (1146, "Table 'your_app.your_model' doesn't exist")</pre>
<p>This error often occurs when you attempt to test a model created using <code>./manage.py inspectdb</code>.</p>
<p>In such cases,</p>
<p><a href="https://www.caktusgroup.com/blog/2010/09/24/simplifying-the-testing-of-unmanaged-database-models-in-django/" target="_blank">Simplifying the Testing of Unmanaged Database Models in Django | Caktus Group</a><br />provides a workaround, but</p>
<p>you can also try the following in your <code>your_app/tests/__init__.py</code>:</p>
<pre style="background-color: #ffffff; color: #000000; font-family: 'Menlo'; font-size: 9.0pt;"><span style="color: #000080; font-weight: bold;">import </span>sys<br /><br /><span style="color: #000080; font-weight: bold;">if </span><span style="color: #008080; font-weight: bold;">'test' </span><span style="color: #000080; font-weight: bold;">in </span>sys.argv:<br />    <span style="color: #808080; font-style: italic;"># For testing<br /></span><span style="color: #808080; font-style: italic;">    </span><span style="color: #000080; font-weight: bold;">from </span>..models <span style="color: #000080; font-weight: bold;">import </span>YourModel<br /><br />    YourModel._meta.managed = <span style="color: #000080; font-weight: bold;">True</span></pre>
<p>Writing this in <code>tests/__init__.py</code> should be sufficient.</p>
