{"id":61187,"date":"2024-10-13T09:09:18","date_gmt":"2024-10-13T01:09:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/61187\/"},"modified":"2024-10-13T09:09:18","modified_gmt":"2024-10-13T01:09:18","slug":"django-multitenant-%e5%88%86%e4%bd%88%e5%bc%8f%e5%a4%9a%e7%a7%9f%e6%88%b6%e6%95%b8%e6%93%9a%e5%ba%ab%e9%a0%85%e7%9b%ae%e5%af%a6%e6%88%b0python-djangopostgrescitus","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/61187\/","title":{"rendered":"Django-Multitenant-\u5206\u4f48\u5f0f\u591a\u79df\u6236\u6578\u64da\u5eab\u9805\u76ee\u5be6\u6230(Python\/Django+Postgres+Citus)"},"content":{"rendered":"<h1 id=\"django-multitenant-%e5%88%86%e4%bd%88%e5%bc%8f%e5%a4%9a%e7%a7%9f%e6%88%b6%e6%95%b8%e6%93%9a%e5%ba%ab%e9%a0%85%e7%9b%ae%e5%af%a6%e6%88%b0python-djangopostgrescitus-ZqdgeOCUTV\">Django-Multitenant-\u5206\u4f48\u5f0f\u591a\u79df\u6236\u6578\u64da\u5eab\u9805\u76ee\u5be6\u6230(Python\/Django+Postgres+Citus)<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u958b\u767c\u4e2d\uff0c\u591a\u79df\u6236\u67b6\u69cb\u8d8a\u4f86\u8d8a\u53d7\u5230\u91cd\u8996\u3002\u9019\u7a2e\u67b6\u69cb\u5141\u8a31\u591a\u500b\u5ba2\u6236\uff08\u79df\u6236\uff09\u5171\u4eab\u540c\u4e00\u500b\u61c9\u7528\u7a0b\u5e8f\u5be6\u4f8b\uff0c\u540c\u6642\u4fdd\u6301\u6578\u64da\u7684\u9694\u96e2\u6027\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528Python\u7684Django\u6846\u67b6\u3001PostgreSQL\u6578\u64da\u5eab\u4ee5\u53caCitus\u64f4\u5c55\u4f86\u5be6\u73fe\u4e00\u500b\u5206\u4f48\u5f0f\u7684\u591a\u79df\u6236\u6578\u64da\u5eab\u9805\u76ee\u3002<\/p>\n<h2 id=\"%e5%a4%9a%e7%a7%9f%e6%88%b6%e6%9e%b6%e6%a7%8b%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-ZqdgeOCUTV\">\u591a\u79df\u6236\u67b6\u69cb\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u591a\u79df\u6236\u67b6\u69cb\u7684\u6838\u5fc3\u5728\u65bc\u6578\u64da\u7684\u9694\u96e2\u548c\u5171\u4eab\u3002\u6bcf\u500b\u79df\u6236\u7684\u6578\u64da\u90fd\u61c9\u8a72\u662f\u7368\u7acb\u7684\uff0c\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u6578\u64da\u7684\u5b89\u5168\u6027\u548c\u96b1\u79c1\u6027\u3002\u5e38\u898b\u7684\u591a\u79df\u6236\u5be6\u73fe\u65b9\u5f0f\u6709\u5169\u7a2e\uff1a<\/p>\n<ul>\n<li><strong>\u6578\u64da\u5eab\u9694\u96e2\uff1a<\/strong>\u6bcf\u500b\u79df\u6236\u64c1\u6709\u81ea\u5df1\u7684\u6578\u64da\u5eab\u3002<\/li>\n<li><strong>\u8868\u9694\u96e2\uff1a<\/strong>\u6240\u6709\u79df\u6236\u5171\u4eab\u540c\u4e00\u6578\u64da\u5eab\uff0c\u4f46\u6bcf\u500b\u79df\u6236\u7684\u6578\u64da\u5b58\u5132\u5728\u4e0d\u540c\u7684\u8868\u4e2d\u3002<\/li>\n<\/ul>\n<p>\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u5c08\u6ce8\u65bc\u4f7f\u7528\u8868\u9694\u96e2\u7684\u65b9\u5f0f\u4f86\u5be6\u73fe\u591a\u79df\u6236\u67b6\u69cb\u3002<\/p>\n<h2 id=\"%e6%8a%80%e8%a1%93%e6%a3%a7%e4%bb%8b%e7%b4%b9-ZqdgeOCUTV\">\u6280\u8853\u68e7\u4ecb\u7d39<\/h2>\n<p>\u6211\u5011\u5c07\u4f7f\u7528\u4ee5\u4e0b\u6280\u8853\u4f86\u69cb\u5efa\u9019\u500b\u591a\u79df\u6236\u7cfb\u7d71\uff1a<\/p>\n<ul>\n<li><strong>Django\uff1a<\/strong>\u4e00\u500b\u9ad8\u6548\u7684Python Web\u6846\u67b6\uff0c\u9069\u5408\u5feb\u901f\u958b\u767c\u3002<\/li>\n<li><strong>PostgreSQL\uff1a<\/strong>\u4e00\u500b\u529f\u80fd\u5f37\u5927\u7684\u958b\u6e90\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff0c\u652f\u6301\u591a\u79df\u6236\u67b6\u69cb\u3002<\/li>\n<li><strong>Citus\uff1a<\/strong>\u4e00\u500bPostgreSQL\u7684\u64f4\u5c55\uff0c\u80fd\u5920\u5c07\u6578\u64da\u5206\u4f48\u5230\u591a\u500b\u7bc0\u9ede\u4e0a\uff0c\u5be6\u73fe\u6c34\u5e73\u64f4\u5c55\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%92%b0%e5%a2%83%e8%a8%ad%e7%bd%ae-ZqdgeOCUTV\">\u74b0\u5883\u8a2d\u7f6e<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5b89\u88ddDjango\u548cPostgreSQL\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u9032\u884c\u5b89\u88dd\uff1a<\/p>\n<pre><code>pip install django psycopg2<\/code><\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u5b89\u88ddCitus\u64f4\u5c55\u3002\u6839\u64da\u4f60\u7684\u64cd\u4f5c\u7cfb\u7d71\uff0c\u5b89\u88dd\u65b9\u6cd5\u53ef\u80fd\u6703\u6709\u6240\u4e0d\u540c\u3002\u4ee5Ubuntu\u70ba\u4f8b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>sudo apt-get install postgresql-12-citus-10.0<\/code><\/pre>\n<h2 id=\"%e5%bb%ba%e7%ab%8bdjango%e9%a0%85%e7%9b%ae-ZqdgeOCUTV\">\u5efa\u7acbDjango\u9805\u76ee<\/h2>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5275\u5efa\u4e00\u500b\u65b0\u7684Django\u9805\u76ee\uff1a<\/p>\n<pre><code>django-admin startproject multitenant_project<\/code><\/pre>\n<p>\u7136\u5f8c\uff0c\u9032\u5165\u9805\u76ee\u76ee\u9304\u4e26\u5275\u5efa\u4e00\u500b\u65b0\u7684\u61c9\u7528\uff1a<\/p>\n<pre><code>cd multitenant_project\npython manage.py startapp tenants<\/code><\/pre>\n<h2 id=\"%e8%a8%ad%e8%a8%88%e6%95%b8%e6%93%9a%e6%a8%a1%e5%9e%8b-ZqdgeOCUTV\">\u8a2d\u8a08\u6578\u64da\u6a21\u578b<\/h2>\n<p>\u5728Django\u7684models.py\u4e2d\uff0c\u6211\u5011\u9700\u8981\u8a2d\u8a08\u4e00\u500b\u79df\u6236\u6a21\u578b\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u79df\u6236\u6a21\u578b\u793a\u4f8b\uff1a<\/p>\n<pre><code>from django.db import models\n\nclass Tenant(models.Model):\n    name = models.CharField(max_length=100)\n    schema_name = models.CharField(max_length=100, unique=True)\n\n    def __str__(self):\n        return self.name<\/code><\/pre>\n<p>\u9019\u500b\u6a21\u578b\u5305\u542b\u79df\u6236\u7684\u540d\u7a31\u548cschema\u540d\u7a31\uff0c\u5f8c\u8005\u5c07\u7528\u65bc\u5728PostgreSQL\u4e2d\u5340\u5206\u4e0d\u540c\u79df\u6236\u7684\u6578\u64da\u3002<\/p>\n<h2 id=\"%e9%85%8d%e7%bd%ae%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-ZqdgeOCUTV\">\u914d\u7f6e\u6578\u64da\u5eab\u9023\u63a5<\/h2>\n<p>\u5728Django\u7684settings.py\u4e2d\uff0c\u6211\u5011\u9700\u8981\u914d\u7f6ePostgreSQL\u7684\u6578\u64da\u5eab\u9023\u63a5\uff1a<\/p>\n<pre><code>DATABASES = {\n    'default': {\n        'ENGINE': 'django.db.backends.postgresql',\n        'NAME': 'your_db_name',\n        'USER': 'your_db_user',\n        'PASSWORD': 'your_db_password',\n        'HOST': 'localhost',\n        'PORT': '5432',\n    }\n}<\/code><\/pre>\n<h2 id=\"%e5%af%a6%e7%8f%be%e5%a4%9a%e7%a7%9f%e6%88%b6%e9%82%8f%e8%bc%af-ZqdgeOCUTV\">\u5be6\u73fe\u591a\u79df\u6236\u908f\u8f2f<\/h2>\n<p>\u70ba\u4e86\u5be6\u73fe\u591a\u79df\u6236\u908f\u8f2f\uff0c\u6211\u5011\u9700\u8981\u5728\u6bcf\u6b21\u8acb\u6c42\u6642\u6839\u64da\u7576\u524d\u7528\u6236\u7684\u79df\u6236\u4fe1\u606f\u4f86\u8a2d\u7f6e\u6578\u64da\u5eab\u9023\u63a5\u3002\u9019\u53ef\u4ee5\u901a\u904e\u4e2d\u9593\u4ef6\u4f86\u5be6\u73fe\uff1a<\/p>\n<pre><code>class TenantMiddleware:\n    def __init__(self, get_response):\n        self.get_response = get_response\n\n    def __call__(self, request):\n        tenant_name = request.headers.get('X-Tenant-Name')\n        request.tenant = Tenant.objects.get(schema_name=tenant_name)\n        return self.get_response(request)<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-ZqdgeOCUTV\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4f7f\u7528Django\u3001PostgreSQL\u548cCitus\uff0c\u6211\u5011\u53ef\u4ee5\u8f15\u9b06\u5730\u69cb\u5efa\u4e00\u500b\u5206\u4f48\u5f0f\u7684\u591a\u79df\u6236\u6578\u64da\u5eab\u7cfb\u7d71\u3002\u9019\u7a2e\u67b6\u69cb\u4e0d\u50c5\u80fd\u5920\u63d0\u9ad8\u61c9\u7528\u7684\u53ef\u64f4\u5c55\u6027\uff0c\u9084\u80fd\u78ba\u4fdd\u6578\u64da\u7684\u5b89\u5168\u6027\u548c\u9694\u96e2\u6027\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u80fd\u548c\u9748\u6d3b\u6027\u7684\u61c9\u7528\u4f86\u8aaa\uff0c\u9019\u662f\u4e00\u500b\u7406\u60f3\u7684\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u53ef\u9760\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u4f86\u90e8\u7f72\u60a8\u7684Django\u61c9\u7528\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u3002\u7121\u8ad6\u662f\u5c0f\u578b\u9805\u76ee\u9084\u662f\u5927\u578b\u4f01\u696d\u61c9\u7528\uff0c\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u4e91\u4f3a\u670d\u5668<\/a> \u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Explore Django-Multitenant for distributed multi-tenant databases using Python, Django, Postgres, and Citus in this practical project guide.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-61187","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/61187","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=61187"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/61187\/revisions"}],"predecessor-version":[{"id":61188,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/61187\/revisions\/61188"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=61187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=61187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=61187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}