ContentType matching query does not exist

Viewed 17397

I recently attempted to load some fixtures into my database. When I run the server and load various pages though I get the error:

Caught DoesNotExist while rendering: ContentType matching query does not exist.

I've tried running syncdb, and resetting each of the apps individually, but haven't had any luck. How do I make this error go away?

8 Answers

I'm surprised no one mentioned the fact that fixture is a list that is being read from first value to last. And it's often possible that there will be a row that mentions a Primary key of a table that will be added latter.

such mistake (generating this extact issue) would look like this in a fixture:

{"model": "lesson", "fields": {"class": 1}},
{
  "model": "class",
  "pk": 1
}

from above example you can see that <Model> matching query query doesn't exist (lesson cannot be added because class with pk=1 has not been added yet). So you have to add classes before you can have lessons, so just switch them up in a fixture.

I found a different cause for this error, I wanted to add in case this helps anyone else. What was causing this issue for me is that I had created a group with specific permissions, and then uninstalled an app that was referenced in the group.

Specifically, I had installed reversion at one point, and created a group called "Site Editor" that gave a user permission to create, edit and delete revisions. Later I un-installed revision, but the group permissions remained when I ran the "dumpdata" command:

[
{
    "fields": {
        "name": "Site Editor",
        "permissions": [
            [
                "add_logentry",
                "admin",
                "logentry"
            ],
            [
                "change_logentry",
                "admin",
                "logentry"
            ],
            [
                "delete_logentry",
                "admin",
                "logentry"
            ],
            [
                "add_group",
                "auth",
                "group"
            ],
            [
                "change_group",
                "auth",
                "group"
            ],
            [
                "delete_group",
                "auth",
                "group"
            ],
            [
                "add_revision",
                "reversion",
                "revision"
            ],
            [
                "change_revision",
                "reversion",
                "revision"
            ],
            [
                "delete_revision",
                "reversion",
                "revision"
            ],
            [
                "add_version",
                "reversion",
                "version"
            ],
            [
                "change_version",
                "reversion",
                "version"
            ],
            [
                "delete_version",
                "reversion",
                "version"
            ],
            [
                "add_session",
                "sessions",
                "session"
            ],
            [
                "change_session",
                "sessions",
                "session"
            ],
            [
                "delete_session",
                "sessions",
                "session"
            ],
            [
                "add_site",
                "sites",
                "site"
            ],
            [
                "change_site",
                "sites",
                "site"
            ],
            [
                "delete_site",
                "sites",
                "site"
            ]
        ]
    },
    "model": "auth.group",
    "pk": 2
}]

When I attempted to run the "loaddata" command, I kept running into this error:

django.core.serializers.base.DeserializationError: 
Problem installing fixture '/Users/me/Documents/Sites/project/path/fixtures/configuration.json': 
ContentType matching query does not exist.

My solution was to simply remove any reference to reversion and versions in the fixture itself, like so:

    [
{
    "fields": {
        "name": "Site Editor",
        "permissions": [
            [
                "add_logentry",
                "admin",
                "logentry"
            ],
            [
                "change_logentry",
                "admin",
                "logentry"
            ],
            [
                "delete_logentry",
                "admin",
                "logentry"
            ],
            [
                "add_group",
                "auth",
                "group"
            ],
            [
                "change_group",
                "auth",
                "group"
            ],
            [
                "delete_group",
                "auth",
                "group"
            ],
            [
                "add_session",
                "sessions",
                "session"
            ],
            [
                "change_session",
                "sessions",
                "session"
            ],
            [
                "delete_session",
                "sessions",
                "session"
            ],
            [
                "add_site",
                "sites",
                "site"
            ],
            [
                "change_site",
                "sites",
                "site"
            ],
            [
                "delete_site",
                "sites",
                "site"
            ]
        ]
    },
    "model": "auth.group",
    "pk": 2
}]

Then I was able to import the fixture without issue.

Have you recently changed to use postgres from mysql?

When querying the contenttypes with something like:

entry_content_type = ContentType.objects.get(
    app_label="entries", model="Entry"
)

This fails because in postgres; app_label and model are all lower case fields. So make use of the __iexact field lookup which will ignore case.

entry_content_type = ContentType.objects.get(
    app_label__iexact="entries", model__iexact="Entry"
)

This is what worked for me after a lot of trials. I was having a lot of trouble with groups and logs.

  • While on sqllite, start server, go to admin and delete all groups. Then type the following in shell:

    python manage.py dumpdata --natural-foreign --natural-primary -e contenttypes -e auth.Permission -e admin.Logentry > datadump_3.json

  • Change settings to MySQL in settings.py

  • python manage.py loaddata datadump_3.json

Related