Django test fixtures and contenttypes

         ·     

Just came across an interesting problem with the contenttypes contributed application and the test framework.

If you are using fixtures in the test frame work you might find that the database gets out of date as you are working on it, particularly if you are creating new models all the time.

To get around this you need to regenerate the contenttypes database. If you depend on this, the following steps could give you problems so make sure that you back up your data (dumpdata) beforehand.

So first off drop the database:

[cc lang=“bash]python manage.py reset contenttypes[/cc]

If the database has foreign key dependancies on contenttypes you will not be able to reset it like that. Use a database administration tool and drop cascade on the django_content_type table instead.

Then sync the database to create a new contenttypes table:

[cc lang=“bash]python manage.py syncdb[/cc]

Now you are ready to dumpdata into a fixture:

[cc lang=“bash]python manage.py dumpdata contenttypes > fixtures/testdata.json[/cc]

comments powered by Disqus