During the 1st HTTP GET (right after the WSGI in daemon mode is reloaded), admin.site.unregister throws a NotRegistered exception, but admin.site.register throws an AlreadyRegistered exception (catch-22?) However, on the subsequent HTTP GETs, everything loads just fine with no error.
Setup:
- Django 1.3
- Apache 2.2
- CentOS
settings.py:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
#'django.contrib.sites', #(this didn't seem to make a difference)
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
'django.contrib.admindocs',
'base',
'sample',
'reports',
'south',
)
models.py:
from django.db import models
from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin as AuthUserAdmin
class Company(models.Model):
name = models.CharField(max_length=256)
class CompanyUser(models.Model):
company = models.ForeignKey(Company)
user = models.OneToOneField(User)
class CompanyUserInline(admin.StackedInline):
model = CompanyUser
max_num = 1
can_delete = False
class CompanyUserAdmin(AuthUserAdmin):
inlines = [ CompanyUserInline ]
# STUCK HERE !!
try:
admin.site.unregister(User)
# throws NotRegistered at / The model User is not registered
except:
admin.site.register(User, CompanyUserAdmin)
# throws AlreadyRegistered at / The model User is already registered
I found the answer from mlavin on freenode’s #django
Moving what belongs in admin.py to admin.py (from models.py) solved it! In my defense though, I inherited the code base – I’m not sure why I would put this snippet in models.py instead of admin.py.
Interesting to note: while this problem never cropped up locally for me via “manage.py runserver” but only when I deployed via Apache + WSGI, one other person mentioned they were able to reproduce it locally. Weird.
But case closed.