{"id":369536,"date":"2024-05-21T04:05:44","date_gmt":"2024-05-21T04:05:44","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=369536"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-05-21T04:05:44","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=369536","title":{"rendered":"<span>\u041c\u0443\u043b\u044c\u0442\u0438-\u0442\u0435\u043d\u0430\u043d\u0442 \u0432 Django<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/2a3\/36e\/64d\/2a336e64def52293019beb68cb4dface.jpg\" width=\"1280\" height=\"720\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/2a3\/36e\/64d\/2a336e64def52293019beb68cb4dface.jpg\" data-blurred=\"true\"\/><\/figure>\n<p><em>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/em> <\/p>\n<p><strong>\u041c\u0443\u043b\u044c\u0442\u0438-\u0442\u0435\u043d\u0430\u043d\u0442<\/strong> (multi-tenancy) \u2014 \u044d\u0442\u043e \u043f\u043e\u0434\u0445\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u0434\u043d\u043e\u043c\u0443 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043e\u0431\u0441\u043b\u0443\u0436\u0438\u0432\u0430\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432 \u0438\u043b\u0438 <em>\u0430\u0440\u0435\u043d\u0434\u0430\u0442\u043e\u0440\u043e\u0432<\/em> (\u0442\u0435\u043d\u0430\u0442\u043e\u0432). <strong>\u041a\u0430\u0436\u0434\u044b\u0439 \u0430\u0440\u0435\u043d\u0434\u0430\u0442\u043e\u0440 \u0438\u0437\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u043d \u043e\u0442 \u0434\u0440\u0443\u0433\u0438\u0445<\/strong>, \u0438\u043c\u0435\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043a\u0430\u0441\u0442\u043e\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u0434 \u0441\u0432\u043e\u0438 \u043d\u0443\u0436\u0434\u044b, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043a\u043e\u0434\u043e\u0432\u043e\u0439 \u0431\u0430\u0437\u043e\u0439 \u0438 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439 \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 \u0432\u0441\u0435\u043c\u0438.<\/p>\n<p>\u041a\u043e\u0433\u0434\u0430 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u0443 \u0437\u0430\u043c\u0435\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044e? \u0415\u0441\u043b\u0438 \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u044b\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u0442\u043e <strong>\u043c\u0443\u043b\u044c\u0442\u0438-\u0442\u0435\u043d\u0430\u043d\u0442 \u043f\u043e\u0434\u0445\u043e\u0434 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0446\u0435\u043d\u0435\u043d \u0434\u043b\u044f SaaS-\u043f\u0440\u043e\u0434\u0443\u043a\u0442\u043e\u0432,<\/strong> \u043a\u043e\u0433\u0434\u0430 \u043e\u0434\u043d\u043e \u0438 \u0442\u043e \u0436\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0440\u0430\u0437\u043d\u044b\u043c \u043a\u043b\u0438\u0435\u043d\u0442\u0430\u043c, \u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u043a\u043b\u0438\u0435\u043d\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441\u043e \u0441\u0432\u043e\u0438\u043c \u043d\u0430\u0431\u043e\u0440\u043e\u043c \u0434\u0430\u043d\u043d\u044b\u0445. \u0412\u0441\u0435 \u044d\u0442\u043e \u0441\u0435\u0440\u044c\u0435\u0437\u043d\u043e \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0442 \u0440\u0435\u0441\u0443\u0440\u0441\u044b \u043d\u0430 \u043e\u0431\u0441\u043b\u0443\u0436\u0438\u0432\u0430\u043d\u0438\u0435 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b, \u0442\u043a \u0432\u0441\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432\u043d\u043e\u0441\u044f\u0442\u0441\u044f \u0446\u0435\u043d\u0442\u0440\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e \u0438 \u043c\u0433\u043d\u043e\u0432\u0435\u043d\u043d\u043e \u0441\u0442\u0430\u043d\u043e\u0432\u044f\u0442\u0441\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u0432\u0441\u0435\u043c \u043a\u043b\u0438\u0435\u043d\u0442\u0430\u043c.<\/p>\n<p>\u0412 Django \u043c\u0443\u043b\u044c\u0442\u0438-\u0442\u0435\u043d\u0430\u043d\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0447\u0430\u0441\u0442\u043e \u0438 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0435\u0441\u0442\u044c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 <strong>django-multitenant<\/strong>.<\/p>\n<h4>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c \u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u043c<\/h4>\n<p>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c <code>django-multitenant<\/code> \u0447\u0435\u0440\u0435\u0437 \u043f\u0438\u043f:<\/p>\n<pre><code class=\"bash\">pip install django-multitenant<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u043f\u043e\u0440\u0430 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c <code>django_multitenant<\/code> \u0432 <code>INSTALLED_APPS<\/code> \u043f\u0440\u043e\u0435\u043a\u0442\u0430 Django \u0432 <code>settings.py<\/code>.<\/p>\n<p>\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0411\u0414, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <code>'ENGINE': 'django_tenants.postgresql_backend'<\/code>, \u0447\u0442\u043e\u0431\u044b \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443, \u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0443 \u0441\u0445\u0435\u043c PostgreSQL:<\/p>\n<pre><code class=\"yaml\">DATABASES = {     'default': {         'ENGINE': 'django_tenants.postgresql_backend',         'NAME': 'your_db_name',         'USER': 'your_db_user',         'PASSWORD': 'your_db_password',         'HOST': 'your_db_host',         'PORT': 'your_db_port',     } }<\/code><\/pre>\n<p>\u041d\u0443\u0436\u043d\u043e \u0442\u0430\u043a\u0436\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c <code>django_tenants<\/code> \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439:<\/p>\n<pre><code class=\"yaml\">INSTALLED_APPS = [     ...     'django_tenants',     ... ]<\/code><\/pre>\n<h4>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u0442\u0435\u043d\u0430\u043d\u0442\u0430\u043c\u0438<\/h4>\n<p>\u0414\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0442\u0435\u043d\u0430\u043d\u0442\u0430 \u0438 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441 \u043d\u0438\u043c \u0434\u043e\u043c\u0435\u043d\u043e\u0432 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u0438 <code>Tenant<\/code> \u0438 <code>Domain<\/code>, \u043a\u0430\u043a \u0431\u044b\u043b\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u043e \u0432 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u0445:<\/p>\n<pre><code class=\"python\"># models.py from django_tenants.models import TenantMixin, DomainMixin  class Client(TenantMixin):     name = models.CharField(max_length=100)  class Domain(DomainMixin):     pass<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0435\u0439, \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u043d\u043e\u0432\u044b\u0445 \u0442\u0435\u043d\u0430\u043d\u0442\u043e\u0432 \u0438 \u0434\u043e\u043c\u0435\u043d\u044b:<\/p>\n<pre><code class=\"python\"># \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0433\u043e \u0442\u0435\u043d\u0430\u043d\u0442\u0430 from your_app.models import Client, Domain  tenant = Client(schema_name='new_tenant', name='New Tenant') tenant.save()  # \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0442\u0435\u043d\u0430\u043d\u0442\u0430  # \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0434\u043e\u043c\u0435\u043d\u0430 \u0434\u043b\u044f \u0442\u0435\u043d\u0430\u043d\u0442\u0430 domain = Domain(domain='newtenant.example.com', tenant=tenant, is_primary=True) domain.save()<\/code><\/pre>\n<p>\u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0439 \u043a \u0441\u0445\u0435\u043c\u0435 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u0442\u0435\u043d\u0430\u043d\u0442\u0430 \u0435\u0441\u0442\u044c \u043a\u043e\u043c\u0430\u043d\u0434\u0430 <code>migrate_schemas<\/code>:<\/p>\n<pre><code class=\"bash\">python manage.py migrate_schemas --schema=new_tenant<\/code><\/pre>\n<p>\u0412 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0442\u0440\u0435\u0431\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0442\u0435\u043d\u0430\u043d\u0442\u043e\u0432:<\/p>\n<pre><code class=\"python\">def create_tenant(user):     new_schema_name = generate_schema_name(user)     new_tenant = Client(schema_name=new_schema_name, name=user.company_name)     new_tenant.save()      domain = Domain(domain=f\"{new_schema_name}.yourdomain.com\", tenant=new_tenant, is_primary=True)     domain.save()<\/code><\/pre>\n<p>\u041c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044f<strong>middleware<\/strong>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043c\u043e\u0433\u0430\u044e\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c, \u043a \u043a\u0430\u043a\u043e\u043c\u0443 \u0442\u0435\u043d\u0430\u043d\u0442\u0443 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0437\u0430\u043f\u0440\u043e\u0441:<\/p>\n<pre><code class=\"python\">MIDDLEWARE = [     'django_tenants.middleware.main.TenantMainMiddleware',     # \u0434\u0440\u0443\u0433\u0438\u0435 middleware... ]<\/code><\/pre>\n<p>\u0412 <code>django-tenants<\/code> \u0435\u0441\u0442\u044c \u0438\u0437\u043e\u043b\u044f\u0446\u0438\u044f static \u0438 media \u0444\u0430\u0439\u043b\u043e\u0432 \u043c\u0435\u0436\u0434\u0443 \u0442\u0435\u043d\u0430\u043d\u0442\u0430\u043c\u0438. \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043f\u0443\u0442\u0435\u0439 \u0434\u043b\u044f \u044d\u0442\u0438\u0445 \u0444\u0430\u0439\u043b\u043e\u0432 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u0432 <code>settings.py<\/code>:<\/p>\n<pre><code class=\"python\">STATIC_URL = '\/static\/' MEDIA_URL = '\/media\/'  # django-tenants \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u0430\u043c\u0438 MULTITENANT_RELATIVE_MEDIA_ROOT = '\/tenant_media\/'<\/code><\/pre>\n<h4>\u0422\u0435\u0441\u0442\u044b<\/h4>\n<p><code>django-tenants<\/code> \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043a\u043b\u0430\u0441\u0441 <code>TenantTestCase<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u0434\u043a\u043b\u0430\u0441\u0441\u043e\u043c <code>django.test.TestCase<\/code>. <code>TenantTestCase<\/code> \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u043e\u0433\u043e \u0442\u0435\u043d\u0430\u043d\u0442\u0430 \u043f\u0435\u0440\u0435\u0434 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435\u043c \u0442\u0435\u0441\u0442\u043e\u0432 \u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u0442 \u0435\u0433\u043e \u043f\u043e\u0441\u043b\u0435:<\/p>\n<pre><code class=\"python\">from django_tenants.test.cases import TenantTestCase  class YourTenantTest(TenantTestCase):     def test_something(self):         # \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439 \u043a\u043e\u0434<\/code><\/pre>\n<p>\u0412\u043d\u0443\u0442\u0440\u0438 <code>TenantTestCase<\/code> \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0442\u0435\u043d\u0430\u043d\u0442\u044b \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0435\u0432, \u0442\u0440\u0435\u0431\u0443\u044e\u0449\u0438\u0445 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0442\u0435\u043d\u0430\u043d\u0442\u0430\u043c\u0438:<\/p>\n<pre><code class=\"python\">from django_tenants.utils import tenant_context  class MultiTenantTest(TenantTestCase):     def test_multi_tenant_interaction(self):         # \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0433\u043e \u0442\u0435\u043d\u0430\u043d\u0442\u0430 \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0430         new_tenant = self.create_tenant(domain_url='newtenant.test.com', schema_name='newtenant')         # \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0442\u0435\u043d\u0430\u043d\u0442\u0430 \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f         with tenant_context(new_tenant):             # \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439 \u043a\u043e\u0434<\/code><\/pre>\n<p>\u041c\u043e\u0436\u043d\u043e \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u044f\u043c\u0438 \u0432 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f\u0445:<\/p>\n<pre><code class=\"python\">from django_tenants.test.cases import FastTenantTestCase  class YourMigrationTest(FastTenantTestCase):     def test_migration(self):         # \u0442\u0435\u0441\u0442<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0435\u0441\u0442\u044c<code> FastTenantTestCase<\/code> \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u043e\u043b\u0435\u0435 \u0431\u044b\u0441\u0442\u0440\u0435\u0439 <code>TenantTestCase<\/code> \u0437\u0430 \u0441\u0447\u0435\u0442 \u043c\u0438\u043d\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0441 \u0431\u0430\u0437\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<h4>\u041c\u043e\u0436\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 DNS<\/h4>\n<p>\u0412 <code>settings.py<\/code> \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c, \u043a\u0430\u043a\u0438\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043e\u0431\u0449\u0438\u043c\u0438 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0442\u0435\u043d\u0430\u043d\u0442\u043e\u0432 <code>SHARED_APPS<\/code> \u0438 \u043a\u0430\u043a\u0438\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0442\u0435\u043d\u0430\u043d\u0442\u0430 <code>TENANT_APPS<\/code>:<\/p>\n<pre><code class=\"python\"># settings.py  SHARED_APPS = [     'django_tenants',  # \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e     'your_app',  # \u0437\u0434\u0435\u0441\u044c \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0438\u043c\u044f \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f     # \u0434\u0440\u0443\u0433\u0438\u0435 \u043e\u0431\u0449\u0438\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f ]  TENANT_APPS = [     'django.contrib.contenttypes',     # \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u0447\u043d\u044b\u0435 \u0434\u043b\u044f \u0442\u0435\u043d\u0430\u043d\u0442\u043e\u0432 ]  INSTALLED_APPS = list(SHARED_APPS) + [app for app in TENANT_APPS if app not in SHARED_APPS]<\/code><\/pre>\n<p>\u0414\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a \u0440\u0430\u0437\u043d\u044b\u043c \u0442\u0435\u043d\u0430\u043d\u0442\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c URL-\u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <code>django-tenants<\/code> URL router:<\/p>\n<pre><code class=\"python\"># urls.py from django.conf.urls import url from django_tenants.utils import tenant_urlpatterns  urlpatterns = [     # URL-\u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 ]  urlpatterns += tenant_urlpatterns([     # URL-\u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u0447\u043d\u044b\u0435 \u0434\u043b\u044f \u0442\u0435\u043d\u0430\u043d\u0442\u043e\u0432 ])<\/code><\/pre>\n<p>\u041d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 DNS \u0434\u043b\u044f \u043f\u043e\u0434\u0434\u043e\u043c\u0435\u043d\u043e\u0432, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0445 \u0440\u0430\u0437\u043d\u044b\u0435 \u0442\u0435\u043d\u0430\u043d\u0442\u044b, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0438 A \u0438\u043b\u0438 CNAME, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0435 \u043d\u0430 IP-\u0430\u0434\u0440\u0435\u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430, \u0433\u0434\u0435 \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d Django-\u043f\u0440\u043e\u0435\u043a\u0442.<\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0434\u043e\u043c\u0435\u043d <code>example.com<\/code>, \u0434\u043b\u044f \u0442\u0435\u043d\u0430\u043d\u0442\u0430 <code>tenant1<\/code> \u0431\u0443\u0434\u0435\u0442 \u0442\u0430\u043a\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c:<\/p>\n<ul>\n<li>\n<p><strong>Type<\/strong>: CNAME<\/p>\n<\/li>\n<li>\n<p><strong>Name<\/strong>: <code>tenant1.example.com<\/code><\/p>\n<\/li>\n<li>\n<p><strong>Value<\/strong>: <code>example.com.<\/code><\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043d\u043e\u0432\u043e\u0433\u043e \u0442\u0435\u043d\u0430\u043d\u0442\u0430 \u043c\u043e\u0436\u043d\u043e \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0434\u043e\u043c\u0435\u043d\u043d\u044b\u0435 \u0437\u0430\u043f\u0438\u0441\u0438 \u0434\u043b\u044f \u043d\u0435\u0433\u043e, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0441\u0438\u0433\u043d\u0430\u043b\u044b Django \u0438\u043b\u0438 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044f \u043c\u0435\u0442\u043e\u0434 <code>save<\/code> \u043c\u043e\u0434\u0435\u043b\u0438 \u0442\u0435\u043d\u0430\u043d\u0442\u0430:<\/p>\n<pre><code class=\"python\"># models.py from django.db.models.signals import post_save from django.dispatch import receiver  @receiver(post_save, sender=Client) def create_domain_for_new_tenant(sender, instance, created, **kwargs):     if created:         Domain.objects.create(domain='{}.example.com'.format(instance.name.lower()), tenant=instance, is_primary=True)<\/code><\/pre>\n<hr\/>\n<p>\u0411\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u0441 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0435\u0439 \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/django-tenants.readthedocs.io\/en\/latest\/\">\u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0437\u0434\u0435\u0441\u044c.<\/a><\/p>\n<p>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043f\u0440\u043e \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0443\u0437\u043d\u0430\u0442\u044c \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 <a href=\"https:\/\/otus.pw\/EKOj\/\">\u043e\u043d\u043b\u0430\u0439\u043d-\u043a\u0443\u0440\u0441\u043e\u0432<\/a> \u043e\u0442 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0443\u044e\u0449\u0438\u0445 \u044d\u043a\u0441\u043f\u0435\u0440\u0442\u043e\u0432 \u043e\u0442\u0440\u0430\u0441\u043b\u0438. <a href=\"https:\/\/otus.pw\/EKOj\/\">\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u0432 \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0435<\/a>.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/797003\/\"> https:\/\/habr.com\/ru\/articles\/797003\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><\/figure>\n<p><em>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/em> <\/p>\n<p><strong>\u041c\u0443\u043b\u044c\u0442\u0438-\u0442\u0435\u043d\u0430\u043d\u0442<\/strong> (multi-tenancy) \u2014 \u044d\u0442\u043e \u043f\u043e\u0434\u0445\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u0434\u043d\u043e\u043c\u0443 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043e\u0431\u0441\u043b\u0443\u0436\u0438\u0432\u0430\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432 \u0438\u043b\u0438 <em>\u0430\u0440\u0435\u043d\u0434\u0430\u0442\u043e\u0440\u043e\u0432<\/em> (\u0442\u0435\u043d\u0430\u0442\u043e\u0432). <strong>\u041a\u0430\u0436\u0434\u044b\u0439 \u0430\u0440\u0435\u043d\u0434\u0430\u0442\u043e\u0440 \u0438\u0437\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u043d \u043e\u0442 \u0434\u0440\u0443\u0433\u0438\u0445<\/strong>, \u0438\u043c\u0435\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043a\u0430\u0441\u0442\u043e\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u0434 \u0441\u0432\u043e\u0438 \u043d\u0443\u0436\u0434\u044b, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043a\u043e\u0434\u043e\u0432\u043e\u0439 \u0431\u0430\u0437\u043e\u0439 \u0438 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439 \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 \u0432\u0441\u0435\u043c\u0438.<\/p>\n<p>\u041a\u043e\u0433\u0434\u0430 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u0443 \u0437\u0430\u043c\u0435\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044e? \u0415\u0441\u043b\u0438 \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u044b\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u0442\u043e <strong>\u043c\u0443\u043b\u044c\u0442\u0438-\u0442\u0435\u043d\u0430\u043d\u0442 \u043f\u043e\u0434\u0445\u043e\u0434 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0446\u0435\u043d\u0435\u043d \u0434\u043b\u044f SaaS-\u043f\u0440\u043e\u0434\u0443\u043a\u0442\u043e\u0432,<\/strong> \u043a\u043e\u0433\u0434\u0430 \u043e\u0434\u043d\u043e \u0438 \u0442\u043e \u0436\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0440\u0430\u0437\u043d\u044b\u043c \u043a\u043b\u0438\u0435\u043d\u0442\u0430\u043c, \u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u043a\u043b\u0438\u0435\u043d\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441\u043e \u0441\u0432\u043e\u0438\u043c \u043d\u0430\u0431\u043e\u0440\u043e\u043c \u0434\u0430\u043d\u043d\u044b\u0445. \u0412\u0441\u0435 \u044d\u0442\u043e \u0441\u0435\u0440\u044c\u0435\u0437\u043d\u043e \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0442 \u0440\u0435\u0441\u0443\u0440\u0441\u044b \u043d\u0430 \u043e\u0431\u0441\u043b\u0443\u0436\u0438\u0432\u0430\u043d\u0438\u0435 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b, \u0442\u043a \u0432\u0441\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432\u043d\u043e\u0441\u044f\u0442\u0441\u044f \u0446\u0435\u043d\u0442\u0440\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e \u0438 \u043c\u0433\u043d\u043e\u0432\u0435\u043d\u043d\u043e \u0441\u0442\u0430\u043d\u043e\u0432\u044f\u0442\u0441\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u0432\u0441\u0435\u043c \u043a\u043b\u0438\u0435\u043d\u0442\u0430\u043c.<\/p>\n<p>\u0412 Django \u043c\u0443\u043b\u044c\u0442\u0438-\u0442\u0435\u043d\u0430\u043d\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0447\u0430\u0441\u0442\u043e \u0438 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0435\u0441\u0442\u044c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 <strong>django-multitenant<\/strong>.<\/p>\n<h4>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c \u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u043c<\/h4>\n<p>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c <code>django-multitenant<\/code> \u0447\u0435\u0440\u0435\u0437 \u043f\u0438\u043f:<\/p>\n<pre><code class=\"bash\">pip install django-multitenant<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u043f\u043e\u0440\u0430 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c <code>django_multitenant<\/code> \u0432 <code>INSTALLED_APPS<\/code> \u043f\u0440\u043e\u0435\u043a\u0442\u0430 Django \u0432 <code>settings.py<\/code>.<\/p>\n<p>\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0411\u0414, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <code>'ENGINE': 'django_tenants.postgresql_backend'<\/code>, \u0447\u0442\u043e\u0431\u044b \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443, \u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0443 \u0441\u0445\u0435\u043c PostgreSQL:<\/p>\n<pre><code class=\"yaml\">DATABASES = {     'default': {         'ENGINE': 'django_tenants.postgresql_backend',         'NAME': 'your_db_name',         'USER': 'your_db_user',         'PASSWORD': 'your_db_password',         'HOST': 'your_db_host',         'PORT': 'your_db_port',     } }<\/code><\/pre>\n<p>\u041d\u0443\u0436\u043d\u043e \u0442\u0430\u043a\u0436\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c <code>django_tenants<\/code> \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439:<\/p>\n<pre><code class=\"yaml\">INSTALLED_APPS = [     ...     'django_tenants',     ... ]<\/code><\/pre>\n<h4>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u0442\u0435\u043d\u0430\u043d\u0442\u0430\u043c\u0438<\/h4>\n<p>\u0414\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0442\u0435\u043d\u0430\u043d\u0442\u0430 \u0438 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441 \u043d\u0438\u043c \u0434\u043e\u043c\u0435\u043d\u043e\u0432 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u0438 <code>Tenant<\/code> \u0438 <code>Domain<\/code>, \u043a\u0430\u043a \u0431\u044b\u043b\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u043e \u0432 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u0445:<\/p>\n<pre><code class=\"python\"># models.py from django_tenants.models import TenantMixin, DomainMixin  class Client(TenantMixin):     name = models.CharField(max_length=100)  class Domain(DomainMixin):     pass<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0435\u0439, \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u043d\u043e\u0432\u044b\u0445 \u0442\u0435\u043d\u0430\u043d\u0442\u043e\u0432 \u0438 \u0434\u043e\u043c\u0435\u043d\u044b:<\/p>\n<pre><code class=\"python\"># \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0433\u043e \u0442\u0435\u043d\u0430\u043d\u0442\u0430 from your_app.models import Client, Domain  tenant = Client(schema_name='new_tenant', name='New Tenant') tenant.save()  # \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0442\u0435\u043d\u0430\u043d\u0442\u0430  # \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0434\u043e\u043c\u0435\u043d\u0430 \u0434\u043b\u044f \u0442\u0435\u043d\u0430\u043d\u0442\u0430 domain = Domain(domain='newtenant.example.com', tenant=tenant, is_primary=True) domain.save()<\/code><\/pre>\n<p>\u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0439 \u043a \u0441\u0445\u0435\u043c\u0435 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u0442\u0435\u043d\u0430\u043d\u0442\u0430 \u0435\u0441\u0442\u044c \u043a\u043e\u043c\u0430\u043d\u0434\u0430 <code>migrate_schemas<\/code>:<\/p>\n<pre><code class=\"bash\">python manage.py migrate_schemas --schema=new_tenant<\/code><\/pre>\n<p>\u0412 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0442\u0440\u0435\u0431\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0442\u0435\u043d\u0430\u043d\u0442\u043e\u0432:<\/p>\n<pre><code class=\"python\">def create_tenant(user):     new_schema_name = generate_schema_name(user)     new_tenant = Client(schema_name=new_schema_name, name=user.company_name)     new_tenant.save()      domain = Domain(domain=f\"{new_schema_name}.yourdomain.com\", tenant=new_tenant, is_primary=True)     domain.save()<\/code><\/pre>\n<p>\u041c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044f<strong>middleware<\/strong>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043c\u043e\u0433\u0430\u044e\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c, \u043a \u043a\u0430\u043a\u043e\u043c\u0443 \u0442\u0435\u043d\u0430\u043d\u0442\u0443 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0437\u0430\u043f\u0440\u043e\u0441:<\/p>\n<pre><code class=\"python\">MIDDLEWARE = [     'django_tenants.middleware.main.TenantMainMiddleware',     # \u0434\u0440\u0443\u0433\u0438\u0435 middleware... ]<\/code><\/pre>\n<p>\u0412 <code>django-tenants<\/code> \u0435\u0441\u0442\u044c \u0438\u0437\u043e\u043b\u044f\u0446\u0438\u044f static \u0438 media \u0444\u0430\u0439\u043b\u043e\u0432 \u043c\u0435\u0436\u0434\u0443 \u0442\u0435\u043d\u0430\u043d\u0442\u0430\u043c\u0438. \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043f\u0443\u0442\u0435\u0439 \u0434\u043b\u044f \u044d\u0442\u0438\u0445 \u0444\u0430\u0439\u043b\u043e\u0432 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u0432 <code>settings.py<\/code>:<\/p>\n<pre><code class=\"python\">STATIC_URL = '\/static\/' MEDIA_URL = '\/media\/'  # django-tenants \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u0430\u043c\u0438 MULTITENANT_RELATIVE_MEDIA_ROOT = '\/tenant_media\/'<\/code><\/pre>\n<h4>\u0422\u0435\u0441\u0442\u044b<\/h4>\n<p><code>django-tenants<\/code> \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043a\u043b\u0430\u0441\u0441 <code>TenantTestCase<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u0434\u043a\u043b\u0430\u0441\u0441\u043e\u043c <code>django.test.TestCase<\/code>. <code>TenantTestCase<\/code> \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u043e\u0433\u043e \u0442\u0435\u043d\u0430\u043d\u0442\u0430 \u043f\u0435\u0440\u0435\u0434 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435\u043c \u0442\u0435\u0441\u0442\u043e\u0432 \u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u0442 \u0435\u0433\u043e \u043f\u043e\u0441\u043b\u0435:<\/p>\n<pre><code class=\"python\">from django_tenants.test.cases import TenantTestCase  class YourTenantTest(TenantTestCase):     def test_something(self):         # \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439 \u043a\u043e\u0434<\/code><\/pre>\n<p>\u0412\u043d\u0443\u0442\u0440\u0438 <code>TenantTestCase<\/code> \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0442\u0435\u043d\u0430\u043d\u0442\u044b \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0435\u0432, \u0442\u0440\u0435\u0431\u0443\u044e\u0449\u0438\u0445 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0442\u0435\u043d\u0430\u043d\u0442\u0430\u043c\u0438:<\/p>\n<pre><code class=\"python\">from django_tenants.utils import tenant_context  class MultiTenantTest(TenantTestCase):     def test_multi_tenant_interaction(self):         # \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0433\u043e \u0442\u0435\u043d\u0430\u043d\u0442\u0430 \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0430         new_tenant = self.create_tenant(domain_url='newtenant.test.com', schema_name='newtenant')         # \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0442\u0435\u043d\u0430\u043d\u0442\u0430 \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f         with tenant_context(new_tenant):             # \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439 \u043a\u043e\u0434<\/code><\/pre>\n<p>\u041c\u043e\u0436\u043d\u043e \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u044f\u043c\u0438 \u0432 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f\u0445:<\/p>\n<pre><code class=\"python\">from django_tenants.test.cases import FastTenantTestCase  class YourMigrationTest(FastTenantTestCase):     def test_migration(self):         # \u0442\u0435\u0441\u0442<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0435\u0441\u0442\u044c<code> FastTenantTestCase<\/code> \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u043e\u043b\u0435\u0435 \u0431\u044b\u0441\u0442\u0440\u0435\u0439 <code>TenantTestCase<\/code> \u0437\u0430 \u0441\u0447\u0435\u0442 \u043c\u0438\u043d\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0441 \u0431\u0430\u0437\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<h4>\u041c\u043e\u0436\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 DNS<\/h4>\n<p>\u0412 <code>settings.py<\/code> \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c, \u043a\u0430\u043a\u0438\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043e\u0431\u0449\u0438\u043c\u0438 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0442\u0435\u043d\u0430\u043d\u0442\u043e\u0432 <code>SHARED_APPS<\/code> \u0438 \u043a\u0430\u043a\u0438\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0442\u0435\u043d\u0430\u043d\u0442\u0430 <code>TENANT_APPS<\/code>:<\/p>\n<pre><code class=\"python\"># settings.py  SHARED_APPS = [     'django_tenants',  # \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e     'your_app',  # \u0437\u0434\u0435\u0441\u044c \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0438\u043c\u044f \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f     # \u0434\u0440\u0443\u0433\u0438\u0435 \u043e\u0431\u0449\u0438\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f ]  TENANT_APPS = [     'django.contrib.contenttypes',     # \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u0447\u043d\u044b\u0435 \u0434\u043b\u044f \u0442\u0435\u043d\u0430\u043d\u0442\u043e\u0432 ]  INSTALLED_APPS = list(SHARED_APPS) + [app for app in TENANT_APPS if app not in SHARED_APPS]<\/code><\/pre>\n<p>\u0414\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a \u0440\u0430\u0437\u043d\u044b\u043c \u0442\u0435\u043d\u0430\u043d\u0442\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c URL-\u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <code>django-tenants<\/code> URL router:<\/p>\n<pre><code class=\"python\"># urls.py from django.conf.urls import url from django_tenants.utils import tenant_urlpatterns  urlpatterns = [     # URL-\u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 ]  urlpatterns += tenant_urlpatterns([     # URL-\u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u0447\u043d\u044b\u0435 \u0434\u043b\u044f \u0442\u0435\u043d\u0430\u043d\u0442\u043e\u0432 ])<\/code><\/pre>\n<p>\u041d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 DNS \u0434\u043b\u044f \u043f\u043e\u0434\u0434\u043e\u043c\u0435\u043d\u043e\u0432, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0445 \u0440\u0430\u0437\u043d\u044b\u0435 \u0442\u0435\u043d\u0430\u043d\u0442\u044b, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0438 A \u0438\u043b\u0438 CNAME, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0435 \u043d\u0430 IP-\u0430\u0434\u0440\u0435\u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430, \u0433\u0434\u0435 \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d Django-\u043f\u0440\u043e\u0435\u043a\u0442.<\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0434\u043e\u043c\u0435\u043d <code>example.com<\/code>, \u0434\u043b\u044f \u0442\u0435\u043d\u0430\u043d\u0442\u0430 <code>tenant1<\/code> \u0431\u0443\u0434\u0435\u0442 \u0442\u0430\u043a\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c:<\/p>\n<ul>\n<li>\n<p><strong>Type<\/strong>: CNAME<\/p>\n<\/li>\n<li>\n<p><strong>Name<\/strong>: <code>tenant1.example.com<\/code><\/p>\n<\/li>\n<li>\n<p><strong>Value<\/strong>: <code>example.com.<\/code><\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043d\u043e\u0432\u043e\u0433\u043e \u0442\u0435\u043d\u0430\u043d\u0442\u0430 \u043c\u043e\u0436\u043d\u043e \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0434\u043e\u043c\u0435\u043d\u043d\u044b\u0435 \u0437\u0430\u043f\u0438\u0441\u0438 \u0434\u043b\u044f \u043d\u0435\u0433\u043e, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0441\u0438\u0433\u043d\u0430\u043b\u044b Django \u0438\u043b\u0438 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044f \u043c\u0435\u0442\u043e\u0434 <code>save<\/code> \u043c\u043e\u0434\u0435\u043b\u0438 \u0442\u0435\u043d\u0430\u043d\u0442\u0430:<\/p>\n<pre><code class=\"python\"># models.py from django.db.models.signals import post_save from django.dispatch import receiver  @receiver(post_save, sender=Client) def create_domain_for_new_tenant(sender, instance, created, **kwargs):     if created:         Domain.objects.create(domain='{}.example.com'.format(instance.name.lower()), tenant=instance, is_primary=True)<\/code><\/pre>\n<hr\/>\n<p>\u0411\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u0441 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0435\u0439 \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/django-tenants.readthedocs.io\/en\/latest\/\">\u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0437\u0434\u0435\u0441\u044c.<\/a><\/p>\n<p>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043f\u0440\u043e \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0443\u0437\u043d\u0430\u0442\u044c \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 <a href=\"https:\/\/otus.pw\/EKOj\/\">\u043e\u043d\u043b\u0430\u0439\u043d-\u043a\u0443\u0440\u0441\u043e\u0432<\/a> \u043e\u0442 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0443\u044e\u0449\u0438\u0445 \u044d\u043a\u0441\u043f\u0435\u0440\u0442\u043e\u0432 \u043e\u0442\u0440\u0430\u0441\u043b\u0438. <a href=\"https:\/\/otus.pw\/EKOj\/\">\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u0432 \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0435<\/a>.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/797003\/\"> https:\/\/habr.com\/ru\/articles\/797003\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-369536","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/369536","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=369536"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/369536\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=369536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=369536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=369536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}