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