Django 管理员注册内联用户配置文件管理员时出现问题

发布于 2024-09-19 05:59:01 字数 1162 浏览 1 评论 0原文

我目前正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人海汹涌 2024-09-26 05:59:30

我的猜测是,您要么正在执行一些疯狂的模块导入...或者...您的 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.messages',
    'django.contrib.admin',

    # your stuff from here on
    'yourproject.userstuff',
)

这样 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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文