Coder Social home page Coder Social logo

xedwin / django-tenancy Goto Github PK

View Code? Open in Web Editor NEW

This project forked from charettes/django-tenancy

0.0 1.0 0.0 507 KB

Handle multi-tenancy in Django with no additional global state using schemas.

Home Page: http://pypi.python.org/pypi/django-tenancy

Python 100.00%

django-tenancy's Introduction

django-tenancy

Handle multi-tenancy in Django with no additional global state using schemas.

https://travis-ci.org/charettes/django-tenancy.png?branch=master https://coveralls.io/repos/charettes/django-tenancy/badge.png?branch=master

Installation

Assuming you have django installed, the first step is to install django-tenancy:

pip install django-tenancy

Now you can import the tenancy module in your Django project.

Using django-tenancy

Define a Tenant Model

The tenant model must be a subclass of tenancy.models.AbstractTenant.

For instance, your myapp/models.py might look like:

from tenancy.models import AbstractTenant

class MyTenantModel(AbstractTenant):
   name = models.CharField(max_length=50)
   # other fields
   def natural_key(self):
      return (self.name, )

Important note: the natural_key method must return a tuple that will be used to prefix the model and its database table. This prefix must be unique to the tenant.

Declare the Tenant Model

Now that you have your tenant model, let's declare in your project in settings.py:

TENANCY_TENANT_MODEL = 'myapp.MyTenantModel'

Run a database synchronization to create the corresponding table:

python manage.py syncdb

Define the tenant-specific models

The tenant-specific models must subclass tenancy.models.TenantModel.

For instance, each tenant will have projects and reports. Here is how myapp/models.py might look like:

from tenancy.models import AbstractTenant, TenantModel

class MyTenantModel(AbstractTenant):
   name = models.CharField(max_length=50)
   # other fields
   def natural_key(self):
      return (self.name, )

class Project(TenantModel):
   name = models.CharField(max_length=50)
   description = models.CharField(max_length=300, blank=True, null=True)

class Report(TenantModel):
   name = models.CharField(max_length=50)
   content = models.CharField(max_length=300, blank=True, null=True)

Playing with the defined models

You can manipulate the tenant and tenant-specific models as any other Django models.

Create a tenant instance

tenant = MyTenantModel.objects.create("myfirsttenant")

Get a tenant-specific model: for_tenant()

<TenantModel>.for_tenant(<AbtractTenantConcreteSubclass instance>)

TenantModel comes with a method that allows you to get the specific AbstractTenantModel for a given Tenant instance. For instance:

tenant_project = Project.for_tenant(tenant)

Create a tenant-specific model instance

tenant_project.objects.create("myfirsttenant_project")

Python 3.5

An issue with circular references between Model objects prevent garbage collection of tenant specific models on tenant deletion.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.