Multi-tenant schema with django

北城余情 提交于 2019-12-08 07:09:06

问题


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.


回答1:


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()



回答2:


I suggest you to use django-tenant-users. This is what they are saying.

This application expands the django users and permissions frameworks to work alongside django-tenant-schemas or django-tenants to allow global users with permissions on a per-tenant basis. This allows a single user to belong to multiple tenants and permissions in each tenant, including allowing permissions in the public tenant. This app also adds support for querying all tenants a user belongs to.

So you can easily manage tenant specific users in django-tenant-schemas or django-tenants.




回答3:


Possibly, in your settings.py, your 'django.contrib.auth' is only configured in your SHARED_APPS, that is why this user only appears in public schema. Please, check to see if you also declared 'django.contrib.auth' in your TENANT_APPS too.



来源:https://stackoverflow.com/questions/41957185/multi-tenant-schema-with-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!