bernardopires / django-tenant-schemas

Tenant support for Django using PostgreSQL schemas.
https://django-tenant-schemas.readthedocs.org/en/latest/
MIT License
1.46k stars 424 forks source link

Example app does not successfully set tenant #580

Closed rkotcherr closed 6 years ago

rkotcherr commented 6 years ago

I am unable to get the sample app to work.

What I did

I downloaded the django-tenant-schemas repository, created a virtualenv in the /examples/tenant_tutorial directory with -p python3 and the following requirements.txt:

Django==2.1.3
django-tenant-schemas==1.9.0
psycopg2==2.7.6.1
psycopg2-binary==2.7.6
pytz==2018.7

I then went to tenant_tutorial/settings.py and set DATABASES.default to my local postgresql endpoint and database name.

I was able to successfully run python manage.py migrate_schemas --shared as described in the documentation, and the following tables were created:

screen shot 2018-11-12 at 7 22 55 pm

I then went to the customers_client table and created the following clients:

screen shot 2018-11-12 at 7 24 05 pm

I then ran ./manage.py runserver and was able to visit the webapp successfully.

The Problem

I am never able to switch tenants. I always see "Current Tenant: None".

One thing I noticed is that I cannot see any print statements that I put in the middleware. I don't know a whole lot about Django yet, so I thought maybe this would be more obvious to somebody else.

I thought it would be appropriate to post here in case there's an update that needs to be made to the example. But most likely it's something I'm not doing quite right, but I'm out of ideas for the moment.

rkotcherr commented 6 years ago

Ok, a made a little progress.

In Django 2.1, MIDDLEWARE_CLASSES should be MIDDLEWARE like this:

MIDDLEWARE = (
    'tenant_schemas.middleware.SuspiciousTenantMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

Now I'm successfully seeing

screen shot 2018-11-12 at 10 25 47 pm

Indicating that no errors have occurred. The app seems to "think" it's connecting to the correct tenants, but in the end I'm still left with only 1 schema:

screen shot 2018-11-12 at 10 26 46 pm

And all users are stored directly into the public schema.

Thoughts?

rkotcherr commented 6 years ago

Just wanted to write to clarify for anybody else who runs across this issue. My problem was that, although I read the docs, I had just manually entered customers into the database. If you're running across the issue I describe above, be sure to add clients as described. ☹️

Regretss commented 6 years ago

Are there any successful cases

Regretss commented 6 years ago

How do you add tenant?

rkotcherr commented 6 years ago

@Regretss You have to create it via the shell. Create a public schema, and then save it. There's an on-save hook that'll create the schema for you. Please read the "Getting started" part of the docs. Everything is described there.

Regretss commented 5 years ago

@rkotcherr Thank you for solving my problem