1. 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:

    python manage.py reset contenttypes

    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:

    python manage.py syncdb

    Now you are ready to dumpdata into a fixture:

    python manage.py dumpdata contenttypes > fixtures/testdata.json
    By timc3 on the
    May 29th, 2009

Please post a comment