django-tenants / django-tenants

Django tenants using PostgreSQL Schemas
MIT License
1.52k stars 342 forks source link

How to populate schemes after creating the tenants #193

Closed marimuthuinchennai closed 6 years ago

marimuthuinchennai commented 6 years ago

Hi,

I need to redirect and populate after successful login for the user to be directed their own tenant. please provide what to do next to redirection and populate the particular tenant.

Thanks

coler-j commented 6 years ago

@marimuthuinchennai can you be more specific? What are you trying to do? Any code samples?

marimuthuinchennai commented 6 years ago

@marimuthuinchennai can you be more specific? What are you trying to do? Any code samples?

Ya Sure,

After creating tenant i need to redirect into particular tenant directly and i need to populate my tenant tables based on user login

marimuthuinchennai commented 6 years ago

@marimuthuinchennai can you be more specific? What are you trying to do? Any code samples?

from django.db import connection schema_name = connection.schema_name

from this i can able to populate public schema and similarly how to populate other schemas

coler-j commented 6 years ago

Have you read the docs for this project? (docs)

To programmatically create a new tenant you would do the following:

from customers.models import Client, Domain

# create your first real tenant
tenant = Client(schema_name='tenant1',
                name='Fonzy Tenant',
                paid_until='2014-12-05',
                on_trial=True)
tenant.save() # migrate_schemas automatically called, your tenant is ready to be used!

# Add one or more domains for the tenant
domain = Domain()
domain.domain = 'tenant.my-domain.com' # don't add your port or www here!
domain.tenant = tenant
domain.is_primary = True
domain.save()

Once that tenant is created you could redirect to a URL / subdomain for the newly created tenant/schema.

You can also act directly on a tenant schema with the following util:

from django_tenants.utils import tenant_context

with tenant_context(tenant):
    # All commands here are ran under the schema from the `tenant` object

# Restores the `SEARCH_PATH` to its original value

- util reference

tomturner commented 6 years ago

I populate my database using celery with a couple of signals. The signal are called 'post_schema_sync' and 'post_schema_sync'

marimuthuinchennai commented 6 years ago

Have you read the docs for this project? (docs)

To programmatically create a new tenant you would do the following:

from customers.models import Client, Domain

# create your first real tenant
tenant = Client(schema_name='tenant1',
                name='Fonzy Tenant',
                paid_until='2014-12-05',
                on_trial=True)
tenant.save() # migrate_schemas automatically called, your tenant is ready to be used!

# Add one or more domains for the tenant
domain = Domain()
domain.domain = 'tenant.my-domain.com' # don't add your port or www here!
domain.tenant = tenant
domain.is_primary = True
domain.save()

Once that tenant is created you could redirect to a URL / subdomain for the newly created tenant/schema.

You can also act directly on a tenant schema with the following util:

from django_tenants.utils import tenant_context

with tenant_context(tenant):
    # All commands here are ran under the schema from the `tenant` object

# Restores the `SEARCH_PATH` to its original value

- util reference

Thank you for your Repsonse

marimuthuinchennai commented 6 years ago

I populate my database using celery with a couple of signals. The signal are called 'post_schema_sync' and 'post_schema_sync'

Hi tom,

can u able to provide the link to use celery

Thanks

kosz85 commented 6 years ago

You should just run the same code to create tenant in celery task. That's all, no magic links for simple tasks. Tenant creation can be longer then request, that's why we are doing it as background task. You can't redirect user directly to not existent tenant, so you will need to implement some kind of waiting page, that will check with ajax calls, if Tenant is ready, and when it's created redirect to it.

marimuthuinchennai commented 6 years ago

You should just run the same code to create tenant in celery task. That's all, no magic links for simple tasks. Tenant creation can be longer then request, that's why we are doing it as background task. You can't redirect user directly to not existent tenant, so you will need to implement some kind of waiting page, that will check with ajax calls, if Tenant is ready, and when it's created redirect to it.

Sorry I went out for Vacation thanks for your reply

vkgautham commented 22 hours ago

@tomturner

I populate my database using celery with a couple of signals. The signal are called 'post_schema_sync' and 'post_schema_sync'

I am curious to know the flow of signals and celery to perform database operations. Since celery tasks are bound before the apps are loaded, database operations on models are not allowed. How do you bind them?

Thanks