{"id":13725570,"url":"https://github.com/charettes/django-tenancy","last_synced_at":"2025-05-12T10:32:24.381Z","repository":{"id":57422306,"uuid":"7954168","full_name":"charettes/django-tenancy","owner":"charettes","description":"Handle multi-tenancy in Django with no additional global state using schemas.","archived":false,"fork":false,"pushed_at":"2017-12-17T06:56:37.000Z","size":528,"stargazers_count":85,"open_issues_count":1,"forks_count":15,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-05-08T01:38:37.680Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://pypi.python.org/pypi/django-tenancy","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/charettes.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-02-01T08:05:26.000Z","updated_at":"2024-04-08T09:14:40.000Z","dependencies_parsed_at":"2022-09-13T14:12:04.491Z","dependency_job_id":null,"html_url":"https://github.com/charettes/django-tenancy","commit_stats":null,"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charettes%2Fdjango-tenancy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charettes%2Fdjango-tenancy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charettes%2Fdjango-tenancy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charettes%2Fdjango-tenancy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/charettes","download_url":"https://codeload.github.com/charettes/django-tenancy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253720091,"owners_count":21952964,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T01:02:27.785Z","updated_at":"2025-05-12T10:32:24.122Z","avatar_url":"https://github.com/charettes.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"**************\ndjango-tenancy\n**************\n\nHandle multi-tenancy in Django with no additional global state using schemas.\n\n.. image:: https://travis-ci.org/charettes/django-tenancy.png?branch=master\n    :target: http://travis-ci.org/charettes/django-tenancy\n\n.. image:: https://coveralls.io/repos/charettes/django-tenancy/badge.png?branch=master\n   :target: https://coveralls.io/r/charettes/django-tenancy\n\nInstallation\n============\nAssuming you have django installed, the first step is to install\n*django-tenancy*:\n\n::\n\n   pip install django-tenancy\n\nNow you can import the ``tenancy`` module in your Django project.\n\nUsing django-tenancy\n====================\n\nDefine a Tenant Model\n---------------------\n\nThe tenant model must be a subclass of ``tenancy.models.AbstractTenant``.\n\nFor instance, your ``myapp/models.py`` might look like:\n\n::\n\n   from tenancy.models import AbstractTenant\n\n   class MyTenantModel(AbstractTenant):\n      name = models.CharField(max_length=50)\n      # other fields\n      def natural_key(self):\n         return (self.name, )\n\n**Important note**: the ``natural_key`` method must return a tuple that will\nbe used to prefix the model and its database table. This prefix must be unique\nto the tenant.\n\nDeclare the Tenant Model\n------------------------\nNow that you have your tenant model, let's declare in your project in\n*settings.py*:\n\n::\n\n   TENANCY_TENANT_MODEL = 'myapp.MyTenantModel'\n\nRun a database synchronization to create the corresponding table:\n\n::\n\n   python manage.py syncdb\n\nDefine the tenant-specific models\n---------------------------------\nThe tenant-specific models must subclass ``tenancy.models.TenantModel``.\n\nFor instance, each tenant will have projects and reports. Here is how\n``myapp/models.py`` might look like:\n\n::\n\n   from tenancy.models import AbstractTenant, TenantModel\n\n   class MyTenantModel(AbstractTenant):\n      name = models.CharField(max_length=50)\n      # other fields\n      def natural_key(self):\n         return (self.name, )\n\n   class Project(TenantModel):\n      name = models.CharField(max_length=50)\n      description = models.CharField(max_length=300, blank=True, null=True)\n\n   class Report(TenantModel):\n      name = models.CharField(max_length=50)\n      content = models.CharField(max_length=300, blank=True, null=True)\n\nPlaying with the defined models\n-------------------------------\nYou can manipulate the tenant and tenant-specific models as any other Django\nmodels.\n\nCreate a tenant instance\n^^^^^^^^^^^^^^^^^^^^^^^^\n::\n\n   tenant = MyTenantModel.objects.create(\"myfirsttenant\")\n\nGet a tenant-specific model: for_tenant()\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\u003cTenantModel\u003e.for_tenant(\u003cAbtractTenantConcreteSubclass instance\u003e)\n\n``TenantModel`` comes with a method that allows you to get the specific\n``AbstractTenantModel`` for a given Tenant instance. For instance:\n\n::\n\n   tenant_project = Project.for_tenant(tenant)\n\nCreate a tenant-specific model instance\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n::\n\n   tenant_project.objects.create(\"myfirsttenant_project\")\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcharettes%2Fdjango-tenancy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcharettes%2Fdjango-tenancy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcharettes%2Fdjango-tenancy/lists"}