问题描述
我目前正在开发一个 django 项目.我正在尝试将 UserProfile 模型内联添加到我的 User 模型中.在我的 models.py 中,我有:
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()
在我的 admin.py 中有:
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
此异常来自 admin.site.unregister(User)
行.
但是,当我注释掉该行时,我得到以下异常:
However, when I comment out that line, I get the following exception:
AlreadyRegistered at /admin
The model User is already registered
我的 django 设置似乎有点两极.我花了一个小时左右的时间研究这个问题,我的代码似乎对其他人来说很好用.有没有人知道为什么会发生这种情况?
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?
谢谢,特拉维斯
推荐答案
我的猜测是你要么正在做一些疯狂的模块导入......或者......你的 settings.INSTALLED_APPS代码>列表.确保
'django.contrib.auth'
出现在列表中,位于替换默认管理员的应用之前.该列表应如下所示:
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',
)
这样 django 的应用程序会注册 User
模型,然后您使用自己的 ModelAdmin
注销并重新注册它.
That way django's app registers the User
model, and then you unregister and re-register it with your own ModelAdmin
.
这篇关于Django 管理员注册内联用户配置文件管理员的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!