diegofsousa / django-multi-tenancy-example

Example of using Django with multi-tenancy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Multi-tenancy Django Example

Example of multi-tenancy using Django 1.11 and MySql.

Step by Step to Install:

First of all, have pip and virtualenv installed.

  1. Create a virtualenv for the project by prescribing Python 3:
    virtualenv --python = python3 [environment name]

  2. Enter the virtualenv folder and activate it:
    source bin/activate

  3. To install the mysqlclient driver make sure you have installed the dependencies below:
    sudo apt-get install python-dev python3-dev
    sudo apt-get install libmysqlclient-dev

  4. Install the required packages:
    pip install -r requirements.txt
    (NOTE) some bugs were found in the django-db-multitenant==0.3.2 library. So a fork was created with the problem already fixed (https://github.com/diegofsousa/django-db-multitenant). The above command already installs the version with bugs fixed.

  5. Create the databases in mysql command line. In the following example we will use two tenants (tenant1 and tenant2), however you can change it with whatever you want. NOTE: Each database will be a tenant. Here is an example of how it should be created:
    CREATE DATABASE tenant1 CHARACTER SET utf8;
    CREATE DATABASE tenant2 CHARACTER SET utf8;

  6. Make the database settings in settings.py as follows:

DATABASES = {
    'default': {
        'ENGINE': 'db_multitenant.db.backends.mysql',
        'NAME': 'tenant',
        'USER': 'YOUR DATABASE USERNAME',
        'PASSWORD': 'YOUR DATABASE PASSWORD',
        'HOST': 'localhost',
        'PORT': 'YOUR DATABASE PORT',
    }
} 

One of the tenants will be used in this configuation only for Django not to complain about syntax error.

  1. Add the hosts file (in Linux OS: /etc/hosts/) for your computer, pointing to 127.0.0.1 as the tenants were created:
127.0.0.1       tenant1.example
127.0.0.1       tenant2.example
  1. Migrate the databases:
    TENANT_DATABASE_NAME=tenant1 ./manage.py migrate
    TENANT_DATABASE_NAME=tenant2 ./manage.py migrate

  2. Run your application with ./manage.py runserver and access your tenants with:
    http://tenant1.example:8000 for tenant1 and http://tenant2.example:8000 for tenant2.

Credits

  • Diego Fernando and Renan Xavier.

About

Example of using Django with multi-tenancy


Languages

Language:Python 100.0%