Django 管理员注册内联用户配置文件管理员时出现问题
我目前正在开发一个 django 项目。我正在尝试将 UserProfile 模型内联添加到我的 User 模型中。在我的 models.py 中,我有:
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 中,我有:
class UserProfileInline(admin.StackedInline):
model = UserProfile
class UserAdmin(admin.ModelAdmin):
inlines = [UserProfileInline]
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
当我运行开发服务器时(是的,我已重新启动它),我收到以下异常:
NotRegistered at /admin
The model User is not registered
此异常来自 admin.site.unregister (用户)
行。
然而,当我注释掉该行时,出现以下异常:
AlreadyRegistered at /admin
The model User is already registered
我的 django 设置似乎有点两极。我花了一个小时左右的时间研究这个问题,我的代码似乎对其他人来说非常有效。有谁知道为什么会发生这种情况?
谢谢, 特拉维斯
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的猜测是,您要么正在执行一些疯狂的模块导入...或者...您的
settings.INSTALLED_APPS
列表中存在排序问题。确保'django.contrib.auth'
出现在列表中要替换默认管理员的应用之前。该列表应如下所示:这样 django 的应用程序会注册
User
模型,然后您可以使用自己的ModelAdmin
取消注册并重新注册它。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:That way django's app registers the
User
model, and then you unregister and re-register it with your ownModelAdmin
.