{"id":537,"date":"2016-09-28T16:11:15","date_gmt":"2016-09-28T08:11:15","guid":{"rendered":"http:\/\/julyclyde.org\/?p=537"},"modified":"2016-09-28T16:11:15","modified_gmt":"2016-09-28T08:11:15","slug":"django-database-router%e6%98%af%e4%b8%aa%e5%a5%bd%e4%b8%9c%e8%a5%bf","status":"publish","type":"post","link":"https:\/\/julyclyde.org\/?p=537","title":{"rendered":"django database router\u662f\u4e2a\u597d\u4e1c\u897f"},"content":{"rendered":"<p>\u4eca\u5929\u7ed9sentry\u52a0\u4e86MySQL\u8bfb\u5199\u5206\u79bb\u673a\u5236\uff0c\u8bb0\u5f55\u4e00\u4e0b\uff1a<\/p>\n<p>DATABASES\u91cc\uff0c\uff08sentry\u7684\u813e\u6c14\u6bd4\u8f83\u602a\uff09\u4fdd\u7559\u540d\u4e3adefault\u7684\u914d\u7f6e\uff0c\u5199master\u6570\u636e\u5e93\u7684\u53c2\u6570\uff1b\u65b0\u589e\u4e00\u4e2a\u540d\u4e3aslave\u7684\u914d\u7f6e\uff0c\u4f7f\u7528\u53ea\u8bfb\u7528\u6237\u540d\u5bc6\u7801\uff0c\u6216\u5f00\u542f\u670d\u52a1\u5668\u7aefread only<br \/>\n\u7136\u540e\u589e\u52a0\u4e00\u4e2a\u7c7b\uff0c\u5e26\u56db\u4e2a\u51fd\u6570\uff1a<\/p>\n<pre class=\"brush: python\">class DatabaseRWSplitRouter(object):\r\n    def db_for_read(self, model, **hints):\r\n        return 'slave'\r\n\r\n    def db_for_write(self, model, **hints):\r\n        return 'default'\r\n\r\n    def allow_relation(self, model1, model2, **hints):\r\n        return True\r\n\r\n    def allow_migrate(self, db, app_label, model_name=None, **hints):\r\n        if db == 'slave':\r\n            return None\r\n        else:\r\n            return True\r\n<\/pre>\n<p>\u518d\u628a\u8fd9\u4e2a\u6ce8\u518c\u8fdb\u53bb<\/p>\n<pre class=\"brush: python\">DATABASE_ROUTERS = (DatabaseRWSplitRouter(), )\r\n<\/pre>\n<p>\u6ce8\u610f\u8fd9\u4e2a\u6ce8\u518c\uff0c\u53ef\u4ee5\u5199\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684dotted_path\u4e5f\u53ef\u4ee5\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u6211\u5077\u61d2\u5c31\u76f4\u63a5\u5199\u4e86\u4e00\u4e2a\u5bf9\u8c61\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u7ed9sentry\u52a0\u4e86MySQL\u8bfb\u5199\u5206\u79bb\u673a\u5236\uff0c\u8bb0\u5f55\u4e00\u4e0b\uff1a DATABASES\u91cc\uff0c\uff08sentry\u7684\u813e\u6c14\u6bd4\u8f83\u602a\uff09\u4fdd\u7559\u540d\u4e3adefault\u7684\u914d\u7f6e\uff0c\u5199master\u6570\u636e\u5e93\u7684\u53c2\u6570\uff1b\u65b0\u589e\u4e00\u4e2a\u540d\u4e3aslave\u7684\u914d\u7f6e\uff0c\u4f7f\u7528\u53ea\u8bfb\u7528\u6237\u540d\u5bc6\u7801\uff0c\u6216\u5f00\u542f\u670d\u52a1\u5668\u7aefread only \u7136\u540e\u589e\u52a0\u4e00\u4e2a\u7c7b\uff0c\u5e26\u56db\u4e2a\u51fd\u6570\uff1a class DatabaseRWSplitRouter(object): def db_for_read(self, model, **hints): return &#8216;slave&#8217; def db_for_write(self, model, **hints): return &#8216;default&#8217; def allow_relation(self, model1, model2, **hints): return True def allow_migrate(self, db, app_label, model_name=None, **hints): if db == &#8216;slave&#8217;: return None else: return True &hellip; <a href=\"https:\/\/julyclyde.org\/?p=537\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[62,15,61],"class_list":["post-537","post","type-post","status-publish","format-standard","hentry","category-default","tag-django","tag-mysql","tag-sentry"],"_links":{"self":[{"href":"https:\/\/julyclyde.org\/index.php?rest_route=\/wp\/v2\/posts\/537","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/julyclyde.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/julyclyde.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/julyclyde.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/julyclyde.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=537"}],"version-history":[{"count":0,"href":"https:\/\/julyclyde.org\/index.php?rest_route=\/wp\/v2\/posts\/537\/revisions"}],"wp:attachment":[{"href":"https:\/\/julyclyde.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/julyclyde.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/julyclyde.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}