Admin 404s on users and dumpdata error: ContentType matching query does not exist

8 views
Skip to first unread message

diN0bot

unread,
Feb 26, 2008, 12:43:55 AM2/26/08
to Django users
yo,

I'd like to update the "Admin 404s on users" and dumpdata error:
ContentType matching query does not exist" for people who, like me,
are searching the web for answers.

From the Admin 404s on users discussion (which I cannot seem to reply
to, hence the new thread; eit)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> ContentType matching query does not exist.

Run this code:

"""
from django.contrib.contenttypes.management import create_contenttypes
from django.db.models import get_apps
for app in get_apps():
create_contenttypes(app, created_models=None, verbosity=2)
"""
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

The update is that instead of
for app in get_apps():
create_contenttypes(app, created_models=None, verbosity=2)
you can call
update_all_contenttypes()
or
for app in get_apps():
update_contenttypes(app, created_models=None, verbosity=2)

you may find it useful to first import your project settings:
import sys, os
from django.core.management import setup_environ
import settings
setup_environ(settings)

So what does it mean for permission and content types to be out of
sync? Let's look at the sql:

[1] the two tables we care about are: auth_permission and
django_content_type

mysql> describe auth_permission;
+-----------------+--------------+------+-----+---------
+----------------+
| Field | Type | Null | Key | Default |
Extra |
+-----------------+--------------+------+-----+---------
+----------------+
| id | int(11) | NO | PRI | NULL |
auto_increment |
| name | varchar(50) | NO | |
| |
| content_type_id | int(11) | NO | MUL |
| |
| codename | varchar(100) | NO | |
| |
+-----------------+--------------+------+-----+---------
+----------------+

mysql> describe django_content_type;
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | | |
| app_label | varchar(100) | NO | MUL | | |
| model | varchar(100) | NO | | | |
+-----------+--------------+------+-----+---------+----------------+

[2] django_content_type.id and auth_permission.content_type_id should
match.

See the pattern in these examples?

mysql> select * from auth_permission;
+----+--------------------------------+-----------------
+---------------------------+
| id | name | content_type_id |
codename |
+----+--------------------------------+-----------------
+---------------------------+
| 1 | Can add message | 1 |
add_message |
| 2 | Can change message | 1 |
change_message |
| 3 | Can delete message | 1 |
delete_message |
| 4 | Can add group | 2 |
add_group |
| 5 | Can change group | 2 |
change_group |
| 6 | Can delete group | 2 |
delete_group |
etc

mysql> select * from django_content_type;
+----+---------------------+--------------+--------------------+
| id | name | app_label | model |
+----+---------------------+--------------+--------------------+
| 1 | message | auth | message |
| 2 | group | auth | group |
| 3 | user | auth | user |
| 4 | permission | auth | permission |
| 5 | content type | contenttypes | contenttype |
| 6 | session | sessions | session |
| 7 | site | sites | site |
| 8 | log entry | admin | logentry |
| 9 | room | bills | room |
etc

For me, I had models showing up in auth_permission that didn't exist
anymore. They weren't in django_content_type. Furthermore, I had
duplicate models with wrong content_types, as if a sync had simply
appended auth_permissions instead of updating the models that were
present.

I don't know how it happened. Maybe switching from sqlite3 to mysql?
maybe switching servers. Something let me corrupt or load faulty data
that I couldn't dump!

I deleted and inserted the appropriate rows in auth_permission,
syncdb'ed and reloaded--and voila, it works again.

Probably it's better not to converse with mysql directly. I couldn't
get the python stuff to work. Maybe others will find it useful, or
will update the python code if appropriate. Regardless, it helps to
have a clearer picture of what's going!

ps - The following sql command list is super useful. Props to the
author.
http://www.pantz.org/software/mysql/mysqlcommands.html

I hope that helps,
diN0bot!

Are you an awesome open source developer who loves collaboration,
activism and fun challenges? Check out
ThoughtAndMemory.org's consumer empowerment web app.
http://thoughtandmemory.org/trac
Join the small but passionate dev flock team as a volunteer, submit a
resume to work full time (grants in process), or intern with us this
summer (boston area college work-study programs may provide funding).
lu...@thoughtandmemory.org
Director of Technology at ThoughtAndMemory.org, a growing non-profit
devoted to providing tools that enable people to make responsible
purchases and informed decisions on a variety of issues, including
global warming, pollution, energy, human rights, health and political
contributions.
Reply all
Reply to author
Forward
0 new messages