Multi-tenant schema with django

↘锁芯ラ 提交于 2019-12-08 15:27:35

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.

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