settings.py:
ALLOWED_HOSTS = ['.myapp.com',]TENANT_APPS = (SHARED_APPS = ('tenant_schemas', # mandatory'customers', # you must list the app where your tenant model resides in'django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles',# your tenant-specific apps'myapp.core',...)
# The following Django contrib apps must be in TENANT_APPS
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# your tenant-specific apps
'myapp.core',
...
)INSTALLED_APPS = SHARED_APPS + TENANT_APPSTENANT_MODEL = "customers.Client"DATABASES = {'default': config('DATABASE_URL',default='postgres://postgres:123@localhost/myapp',cast=db_url),}DATABASES['default']['ENGINE'] = 'tenant_schemas.postgresql_backend'
admin.py
from django.contrib.auth.models import Userfrom django.db import connectiondef create_admin(self, request, queryset):
from models import Clientfor obj in queryset:
# define qual o schema será utilizado para criar o super userconnection.set_schema(obj.schema_name, include_public=False)user = User.objects.create_user('superusername', 'superuseremail', 'superuserpassword')user.is_staff = Trueuser.is_active = Trueuser.is_superuser = Trueuser.save()
>>>> aqui você pode preencher outros models da forma que quiser.
# retorna o schema públicoconnection.set_schema_to_public()create_admin.short_description = "Criar administrador"class ClientAdmin(admin.ModelAdmin):...actions = [create_admin]admin.site.register(Client, ClientAdmin)
models.py
from django.db import modelsfrom tenant_schemas.models import TenantMixinclass Client(TenantMixin):name = models.CharField(max_length=100)paid_until = models.DateField()on_trial = models.BooleanField()created_on = models.DateField(auto_now_add=True)# default true, schema will be automatically created# and synced when it is savedauto_create_schema = Truedef __unicode__(self):return unicode(self.name)class Meta:verbose_name = u'Cliente'verbose_name_plural = u'Clientes'
python manage.py sync_schemas
from customers.models import Client
# create your public tenant
tenant = Client(domain_url='my-domain.com', # don't add your port or www here! on a local server you'll want to use localhost here
schema_name='public',
name='Schemas Inc.',
paid_until='2016-12-05',
on_trial=False)
tenant.save()