python - Django 1.5 自定义用户模型加上 admin.autodiscover() 破坏了应用程序

标签 python django django-admin django-authentication

我的 Django 1.5c1 项目(当前在 Django 1.5 的最新版本上运行)中有一个自定义用户模型(它实际上名为 User 因为我没有看到任何需要以其他方式命名它) github 上的分支)。 AUTH_USER_MODEL 在我的设置中正确定义,因此 auth 模块工作正常,我可以正常登录等。

但是,启用自定义用户模块后,管理站点将无法工作。当我将 admin.autodiscover() 添加到 urls.py 时,网站上的每个页面(不仅仅是管理页面)都会引发 NotRegistered 异常并显示模型用户未注册。回溯显示 admin.autodiscover() 正在尝试调用 admin.site.unregister(User),显然是在注册该模型之前。

我尝试将我的用户模型重命名为 User 以外的名称,但似乎不起作用。我还尝试为该应用程序创建自己的 admin.py ,然后尝试使用指定的自定义 UserAdmin 模型手动注册我的自定义 User 模型在 admin.autodiscover() 运行之前的 admin.py 中,但这实际上导致了一个单独的异常,表明 User 已经注册。

接下来我应该尝试什么才能让 admin.autodiscover() 正常工作?

最佳答案

如果您希望自定义用户模型与管理员一起使用,您似乎需要跳过一些额外的麻烦。来自 documentation :

...your User model must define some additional attributes and methods. These methods allow the admin to control access of the User to admin content:

class models.CustomUser

is_staff True if the user is allowed to have access to the admin site.

is_active True if the user account is currently active.

has_perm(perm, obj=None) True if the user has the named permission.

has_module_perms(app_label) True if the user has perm to access models in the given app.

关于python - Django 1.5 自定义用户模型加上 admin.autodiscover() 破坏了应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14948810/

相关文章:

django - 使用django-admin界面订购商品

python - 找到具有最大乘积的一组整数的子集

python - 如何通过 Restful 服务调用进行单点登录 (SSO)

python - 如何在django admin的注册表中删除用户名字段?

python - django - 值更改后自动更新日期

Django admin ImageField : Upload a valid image. 您上传的文件不是图像或损坏的图像

python - 原型(prototype)文件.proto : A file with this name is already in the pool

python 3 : Does mysql db connection need to be explicitly closed in function?

django - django 中的related_name 和related_query_name 是什么?

python - 我自己的方法用于 list_display 和值作为 bool 图标