How to configure Settings.py to use Django-tenant-schema?

Asked

Viewed 208 times

2

I need to configure my Django to run llb django-tenant-schema

My project does not create schemas and soon I cannot synchronize my bank with the project.

I’m having trouble understanding how you are llb works and how I should configure it, since I tried to configure it based on the tutorials of the own llb. Any hint of what’s missing?!


  1. My database is already structured. (Postgressql)
  2. Man project is almost ready (Just a few adjustments after finishing this part).
  3. Model of schema is also already ready.
  4. If applicable, should use django-tenants? Seems very similar...

I wonder if how I am configuring it is correct, because in my view I am doing it wrong. Even when trying to create data to test is not working as it should.

This is my py Settings.:

SHARED_APPS = (
    'tenant_schemas',  # mandatory, should always be before any django app
    'dash',  # you must list the app where your tenant model resides in

    'django.contrib.contenttypes',

    # everything below here is optional
    # 'django.contrib.auth',
    # 'django.contrib.sessions',
    # 'django.contrib.sites',
    # 'django.contrib.messages',
    # 'django.contrib.admin',
)

TENANT_APPS = (
    'django.contrib.contenttypes',

    # your tenant-specific apps
    'dash',
)

INSTALLED_APPS = [
    'tenant_schemas',  # mandatory, should always be before any django app

    'dash',
    'django.contrib.contenttypes',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'widget_tweaks',
    'login',
    'api',
    'correlator',  # Nome do projeto
]

TENANT_MODEL = "login.LoginBd"  # app.Model

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

DATABASE_ROUTERS = ('tenant_schemas.routers.TenantSyncRouter',)
DATABASES = {
    'default': {
        'ENGINE': 'tenant_schemas.postgresql_backend',  # O resto que não está aqui é desnecessario

Structure of the Project is like this: Projeto

Loginbd must have auto_create_schema = True? Should I set up something else in the templates? settings.py is configured correctly? How do I enter new schemas? And test data?

On the Django-tenant-schema website I saw something that would be like TENANT_MODEL_DOMAIN. Should I use? If yes, how?

3 answers

2


Hello I don’t know if it’s exactly the answer you expect.

You will need to adapt your entire project.

I initially use Django-tenant-schemas(not Django-tenant) and Django-tenant-users, create a Model that inherits the Tenantbase class responsible for creating the Tenants(Schemas in the database). Example:

> #settings.py TENANT_MODEL = loginbd.LoginBD
> 
> #loginbd/models.py
>         class LoginBd(TenantBase): # Este modelo cadastra os Tenants(Áreas)
>              name = models.CharField(max_length=50,
>                                 default=None,
>                                 verbose_name="Nome")

then you need to create the model to create the users who will have access to the tenants, that there will be a group in tenant_public and a user group separated by tenant(schemas) that will be created in your previous model register.

> #settings.py AUTH_USER_MODEL = 'users.TenantUser'
> 
> #users/models.py
>     class TenantUser(UserProfile):
>          name = models.CharField('Name', max_length=100,)

among other settings and adaptations. Remembering that SHARED_APPS applications are for all tenants and TENANT_APPS will have its particular areas per tenant(schema).

0

To configure django-tenant-schemas the instructions are in the repository from the github and on readthedoc, follow all the instructions provided to make sure you haven’t forgotten anything

  • 1

    Sorry if I’m rude but, you don’t think if I created a post on stackoverflow I haven’t read the documentation at least 10 times and tried everything I think I can? Where do you think I got the name from the library? If you had opened the same link you sent, you would realize that I am trying to set up the way they explain, but without success...

  • No problem but your question is how to set up and in the documentation already has how to do, if you are having some error you should detail so that other people who are going to answer at least understand what your problem is, you must be forgetting something, try to look at the project issues if someone has the same problem and if nothing goes right do it all over again

  • Of course, no problem I supplement the question, but so, what you answered is useless. I followed the steps (badly explained) of the library and did not get to work, who has knowledge about this library, will hit his eye in my code and will know what has to be done, and that is what this forum is for, to share knowledge.... And to state the obvious is not intelligent. Now if you had asked to supplement the question I would understand.

  • I still do not have this privilege kkkk, has this article from Vinta that a company specialized in Django, they explain a solution that already has in Django https://www.vinta.com.br/blog/2017/multitenancy-juggling-customer-datadjango/

  • I think you have confused things, in my view this is a way that they have created to do without Ilb, and I believe it is better to do via llb stable. But thanks just the same.

0

Loginbd must have auto_create_schema = True?

This option should serve for after saving the registration it create all database structure, by default is True.

On the website of Django-tenant-schema I saw something that would be like TENANT_MODEL_DOMAIN. Should I use? If so, how?

This option should be entered in Settings.py, it points out which domain your tenants will use, for example:

tenant1.exemplo.com.br
tenant2.exemplo.com.br

Example of configuration in Settings.py

TENANT_MODEL_DOMAIN = 'exemplo.com.br'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.