使用django的多租户架构 [英] Multi-tenant schema with django

查看:660
本文介绍了使用django的多租户架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用django处理多租户架构是我的新手.我遵循以下链接 https://django-tenant-schemas.阅读thedocs.io/en/latest/install.html

I am new to working with multi-tenant schema with django. I have followed the below link https://django-tenant-schemas.readthedocs.io/en/latest/install.html

当我创建客户端对象时,会创建单独的租户架构,这很好.但是跟随的用户不是在单独的租户架构中创建的,而是仅在公共架构中创建的. 我的看法:

When I creating client object, separate tenant schema is created,it's fine. but followed user is not created in separate tenant schema, it's created only in public schema. my view:

def registration(request):
   form = RegistrationForm()
   if request.method == 'POST': # Post method
      company_name = request.POST['company_name']
      website = request.POST['website']
      username = request.POST['username']
      f_name = request.POST['first_name']
      l_name = request.POST['last_name']
      email = request.POST['email']
      password = request.POST['password']
      confirm_password = request.POST['confirm_password']
      try:
         """ create Client for tenant schema"""
         client =Client()
         client.domain_url = 'company1.user.com'
         client.schema_name = username
         client.name = company_name
         client.save()

         """ create user"""
         user = User()
         user.username = username
         user.first_name = f_name
         user.last_name = l_name
         user.email = email
         user.set_password(password)
         user.is_active = True
         user.is_staff = True
         user.save()

并且我想在用户登录名从公共租户重定向到其私人客户租户帐户时更改域网址.

and I wanna change the domain url when a user login's in it redirects from the public tenant to their private client tenant account.

我对这种功能还很陌生.

I am very new to this kind of functionality.

任何人都可以给我一些指导或解决方案.

Any one can give me some guidelines or solution.

推荐答案

在创建用户之前,将数据库模式设置为新创建的数据库模式.最好的方法是使用 schema_context 上下文管理器.像这样:

Set your db schema to the newly created one, before creating the user. The best way to do it is using the schema_context context manager. Something like this:

from tenant_schemas.utils import schema_context

with schema_context(client.schema_name):

    user = User()
    user.username = username
    user.first_name = f_name
    user.last_name = l_name
    user.email = email
    user.set_password(password)
    user.is_active = True
    user.is_staff = True
    user.save()

这篇关于使用django的多租户架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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