在第一次 HTTP GET 期间(在重新加载守护程序模式下的 WSGI 之后),admin.site.unregister 抛出 NotRegistered 异常,但 admin.site.register 抛出 AlreadyRegistered 异常(catch-22?)但是,在随后的 HTTP GET 中,一切都加载得很好,没有错误。
设置:
- 姜戈 1.3
- 阿帕奇 2.2
- 中央操作系统
设置.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',
)
模型.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