【发布时间】:2018-04-25 07:30:38
【问题描述】:
我正在做一个需要 3 种用户类型的项目。
- 管理员
- 供应商
- 客户
我希望为所有三个供应商和客户拥有单独的模型,而不是在通用用户模型中拥有类型字段。
我解决这个问题的第一个方法是通过对 AbstractUser 模型进行子类化来定义所有模型
# models.py
from django.contrib.auth.models import AbstractUser
class Customer(AbstractUser):
pass
class Vendor(AbstractUser):
pass
并添加自定义的Authentication后端,根据请求对象处理用户的身份验证。
# settings.py
AUTHENTICATION_BACKENDS = ['backends.CustomAuthBackend']
我的 backends.py 文件将包含验证用户的逻辑,并根据请求对象为每个用户使用不同的模型。
# backends.py
from __future__ import print_function
class CustomAuthBackend(object):
def authenticate(self, request, username=None, password=None):
# authenticate user based on request object
def get_user(self, user_id):
# logic to get user
但这不起作用,看起来我还需要在 settings.py 中指定用于身份验证的 AUTH_USER_MODEL。
有可能吗?Django 是否允许从 3 个不同的表进行身份验证。我该怎么做呢?有没有其他方法可以解决这个问题,我应该改变什么?
【问题讨论】:
标签: python django django-models django-authentication