用admin auth注册自定义用户模型 [英] Register custom user model with admin auth

查看:58
本文介绍了用admin auth注册自定义用户模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django项目中,我有一个自定义用户模型,该模型添加了一个额外的字段:

In a Django project, I have a custom user model that adds one extra field:

class User(AbstractUser):

    company = models.ForeignKey(Company, null=True, blank=True)

此模型是在我的应用中定义的,例如 MyApp.models .

This model is defined in my app, say MyApp.models.

如何获取新的 User 模型作为原始的 django.contrib.auth 模型显示在身份验证和授权"下?

How can I get the new User model to show up under "Authentication and Authorization" as the original django.contrib.auth model?

推荐答案

我认为您正在寻找的是替换django用户模型.为此,请参见此帖子的答案:扩展用户Django中具有自定义字段的模型.我建议您走扩展路线,但这意味着父子模型都必须注册.

I think what you're looking for is replacing the django user model. To do this, see the answer on this post: Extending the User model with custom fields in Django. I would suggest going the extending route, but that would mean both parent and child model would have to be registered.

如果您真的想要一个模型,只需将 AUTH_USER_MODEL 设置设置为您的模型.这实际上替代了默认模型.

If you really want one single model, just set the AUTH_USER_MODEL setting to your model. This essentially replaces the default model.

在您的settings.py中:

In your settings.py:

AUTH_USER_MODEL = "appname.UserModelName"

有关替换用户模型的更多信息,请参见 https://docs.djangoproject.com/en/dev/topics/auth/customizing/#substituting-a-custom-user-model .

For more info on replacing the user model, see https://docs.djangoproject.com/en/dev/topics/auth/customizing/#substituting-a-custom-user-model.

这篇关于用admin auth注册自定义用户模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆