It’s occasionally useful to be able to create a Django model class in your unit test suite. Let’s
say you’re building a library which creates an
which your users will want to subclass. There’s no need for your library to subclass it, but your
library should still test that you can create a subclass and test out its features. If you create
that model in your
models.py file, then Django will think that it is a real part of your library and
load it whenever you (or your users) call
syncdb. That’s bad.
The solution is to create it in a
file within your Django app. If it’s not in
models.py, Django won’t load it during
1 2 3 4 5 6 7 8 9 10 11
A problem with this solution is that I rarely use a single
tests.py file. Instead we use multiple
test files collected in a tests package. If you try to create a model in
this approach fails because Django tries to create the model in an application named
there is no such app in INSTALLED_APPS. The solution is to set
app_label to the name of your app
in an inner Meta class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Oh, and I almost forgot… if you use South,
this might not work, unless you set
False in your settings file.
Comments and corrections welcome!