如何使用自定义用户模型实现多租户?

How to implement multi-tenancy with custom user model?

我正在尝试创建一个多租户应用程序,其中通过 django 默认用户模型进行身份验证。我使用了自己的自定义用户模型。我想对多个模式使用同一个数据库。

User Model

class UsersManager(BaseUserManager):
    def create_user(self, username, password=None, **kwargs):
        if not username:
            raise ValueError('Users must have a valid username.')

        if not kwargs.get('email'):
            raise ValueError('Users must have a valid email.')

        if not kwargs.get('contact_number'):
            raise ValueError('Users must have a valid contact number.')

        account = self.model(
            username=username, email = kwargs.get('email'), contact_number=kwargs.get('contact_number'),first_name=kwargs.get('first_name'),last_name=kwargs.get('last_name')
        )

        account.set_password(password)
        account.save()

        return account

    def create_staffuser(self, username, password, **kwargs):
        account = self.create_user(username, password, **kwargs)

        account.is_admin = False
        account.is_superuser = False
        account.is_staff = True
        account.is_agent = False
        account.save()

        return account

    def create_superuser(self, username, password, **kwargs):
        account = self.create_user(username, password, **kwargs)

        account.is_admin = True
        account.is_staff = True
        account.is_superuser = True
        account.is_agent = False
        account.save()

        return account

    def create tenant_user(self, username, password, **kwargs):
       """Custom command to create a tenant user"""
       ...   ?????????????????????


class User(AbstractBaseUser, PermissionsMixin):
    first_name = models.CharField(max_length=50, default=None, null=True)
    middle_name = models.CharField(max_length=50, default=None, null=True)
    last_name = models.CharField(max_length=50, default = None, null=True)
    username = models.CharField(unique=True, max_length=50)
    email = models.EmailField(unique=True)
    street_address = models.CharField(max_length=150)
    city = models.CharField(max_length=50)
    contact_number = models.CharField(max_length=40)
    alternative_contact_number = models.CharField(max_length=40,default=None, null=True)
    created_at = models.DateTimeField(default=timezone.now)
    updated_at = models.DateTimeField(default=timezone.now)


    objects = UsersManager()

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['email','first_name','last_name','city','contact_number']

    is_staff = models.BooleanField(default=False)
    is_admin = models.BooleanField(default=False)
    is_agent = models.BooleanField(default=False)
    is_customer = models.BooleanField(default=False)
    is_tenant = models.BooleanField(default=False)

TenantModel

class Client(TenantMixin):
    user = models.ManyToMany('User', related_name='hospital')
    paid_until = models.DateField()
    on_trial = models.BooleanField()
    created_on = models.DateField(auto_now_add=True)

    auto_create_schema = True

我正在使用用户模型在 TenantModel 中创建多对多关系。我想要达到的目标:

all in the docs。您遗漏了代码中最重要的部分,即您的配置 settings.py。如果您希望 auth 是全局的,那么您需要将 auth 放在共享架构中:

settings.py

SHARED_APPS = [
    'django_tenants',

    'django.contrib.contenttypes',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'apps.client',
    'apps.user_app_goes_here',
]

TENANT_APPS = [
    'apps.other_tenant_specific_apps',

    'more_3rd_party_apps',
]

身份验证发生在您定义它的地方。在上面的设置中,我有租户特定的应用程序,这些应用程序只能由特定租户访问,但 authuser_modelclient 是在共享架构上处理的。

您可能 运行 进入的事物:

如果您这样做,如果您试图让所有用户只能访问他们自己的模式数据,则您将必须编写一个自定义中间件来将用户重定向到正确的域(根据正确的模式运行)。也就是说,您需要登录到正确的子域才能访问特定于租户的数据。

您不需要通过 CLI 创建租户。这也可以通过加载要写入的模式来完成。这也都在文档中。

您可能找不到关于此包的大量帮助 - 至少这是我使用它的历史。