我的 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
block 引用>
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/