且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Django中使用多个用户模型?

更新时间:2022-10-23 13:05:59

第一个用户类型是使用用户名和密码登录的管理员.其次,使用电话号码和密码登录的客户.只有客户具有自定义字段.

您不希望有多个用户模型(疯狂,而且Django并没有真正支持该模型).您需要多个身份验证后端./p>

使用(可空的)电话号码字段输入用户模型.并在您的应用程序中添加使用电话号码+密码而不是用户名+密码的auth后端.

I'm trying to use two user models with different 'username' type each one.

First user type is an administrator who login in with username and password. Second, a customer who login with phone number and password. Only the customer has custom fields.

I tried customize the Django User model but it only allows one auth user model.

I am thinking use an authentication backend.

Administrator only will login in admin dashboard, while customer only login in application.

Edit.

Different problem from. My user types have different ways to login each other.

My solution: Use Django's user model for dashboard administrator only. Use a Customer model with custom authentication backend.

First user type is an administrator who login in with username and password. Second, a customer who login with phone number and password. Only the customer has custom fields.

You don't want multiple user models (crazy and not really supported in Django anyway). You want multiple authentication backends.

Put a User model with a (nullable) phone number field. And add to your application an auth backend which uses phone number + password instead of username + password.