问题与Django管理员注册在线用户配置文件管理员 [英] Issue with Django admin registering an inline user profile admin
问题描述
class UserProfile(models.Model):
'''
在django管理员中扩展到用户模型。
'''
user = models.ForeignKey(User)
site_role = models.CharField(max_length = 128,choices = SITE_ROLE)
signature = models.CharField(max_length = 128 )
position_title = models.CharField(max_length = 128)
on_duty = models.BooleanField(default = False)
on_duty_order = models.IntegerField()
在我的admin.py中,我有:
class UserProfileInline(admin.StackedInline):
model = UserProfile
class UserAdmin(admin.ModelAdmin):
inlines = [UserProfileInline]
admin.site.unregister(User)
admin.site.register(User,UserAdmin)
当我运行开发服务器(是的,我重新启动它)我得到以下异常:
未注册在/ admin
模型用户未注册
此异常来自 admin.site.unregister(用户)
但是,当我注释掉该行时,我得到以下异常:
已经注册在/ admin
模型用户已经注册
关于我的django设置似乎是一个双极性。我花了一个小时左右的时间来研究这个问题,而我的代码似乎对其他人来说非常有用。有没有人有什么洞察为什么会发生这种情况?
谢谢,
Travis
我的猜测是,您正在做一些疯狂的模块导入...或...您的 settings.INSTALLED_APPS
列表。确保您的应用程序正在替换默认管理员之前,您的列表中将出现'django.contrib.auth'
。列表应该如下所示:
INSTALLED_APPS =(
#django apps first
'django。 contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib。消息',
'django.contrib.admin',
#你的东西从这里
'yourproject.userstuff',
)
django的应用程序注册用户
模型,然后取消注册重新注册您自己的 ModelAdmin
。
I'm currently working on a django project. I'm attempting to add a UserProfile model inline to my User model. In my models.py I have:
class UserProfile(models.Model):
'''
Extension to the User model in django admin.
'''
user = models.ForeignKey(User)
site_role = models.CharField(max_length=128, choices=SITE_ROLE)
signature = models.CharField(max_length=128)
position_title = models.CharField(max_length=128)
on_duty = models.BooleanField(default=False)
on_duty_order = models.IntegerField()
In my admin.py I have:
class UserProfileInline(admin.StackedInline):
model = UserProfile
class UserAdmin(admin.ModelAdmin):
inlines = [UserProfileInline]
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
When I run the development server (yes, I have restarted it) I get the following exception:
NotRegistered at /admin
The model User is not registered
This exception is coming from the admin.site.unregister(User)
line.
However, when I comment out that line, I get the following exception:
AlreadyRegistered at /admin
The model User is already registered
Something about my django setup seems to be a little bi-polar. I've spent an hour or so researching this problem and the code I have seems to work great for others. Does anyone have any insight into why this might be happening?
Thanks, Travis
my guess is that you either are doing some crazy module importing... or... you have an ordering problem in your settings.INSTALLED_APPS
list. Make sure that 'django.contrib.auth'
appears on your list before your app that is replacing the default admin. The list should look something like this:
INSTALLED_APPS = (
# django apps first
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.admin',
# your stuff from here on
'yourproject.userstuff',
)
That way django's app registers the User
model, and then you unregister and re-register it with your own ModelAdmin
.
这篇关于问题与Django管理员注册在线用户配置文件管理员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!