{"id":200161,"date":"2025-05-06T11:07:23","date_gmt":"2025-05-06T03:07:23","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200161\/"},"modified":"2025-05-06T11:07:23","modified_gmt":"2025-05-06T03:07:23","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-python-orm-%e5%ba%93%e6%9e%84%e5%bb%ba%e6%95%b0%e6%8d%ae%e6%a8%a1%e5%9e%8b%ef%bc%8c%e9%81%bf%e5%85%8d%e6%89%8b%e5%8a%a8%e7%bc%96%e5%86%99%e6%a8%a1%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200161\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Python ORM \u5e93\u6784\u5efa\u6570\u636e\u6a21\u578b\uff0c\u907f\u514d\u624b\u52a8\u7f16\u5199\u6a21\u578b\u7c7b\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 Python ORM \u5e93\u6784\u5efa\u6570\u636e\u6a21\u578b\uff0c\u907f\u514d\u624b\u52a8\u7f16\u5199\u6a21\u578b\u7c7b\uff1f<\/h1>\n<p>\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662f\u4e3b\u673a\u5b9d\u8d1d\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729932711671cada71aa66.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u65e0\u9700\u7f16\u5199\u6a21\u578b\u7c7b\u7684 python orm \u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u5728 python \u4e2d\u6784\u5efa\u6570\u636e\u6a21\u578b\u65f6\uff0c\u7f16\u5199\u8be6\u7ec6\u7684\u6a21\u578b\u7c7b\u662f\u5e38\u89c1\u7684\u505a\u6cd5\u3002\u7136\u800c\uff0c\u8bb8\u591a orm \u5e93\u63d0\u4f9b\u4e86\u66ff\u4ee3\u65b9\u6848\uff0c\u4ece\u800c\u7701\u53bb\u4e86\u624b\u52a8\u7f16\u5199\u8fd9\u4e9b\u6a21\u578b\u7c7b\u7684\u9ebb\u70e6\u3002<\/p>\n<p><strong>\u67b6\u6784\u4f18\u5148\uff08dbfirst\uff09\u65b9\u6cd5<\/strong><\/p>\n<p>\u5728\u67b6\u6784\u4f18\u5148\u6216 dbfirst \u65b9\u6cd5\u4e2d\uff0c\u60a8\u5148\u521b\u5efa\u6570\u636e\u5e93\u8868\u7ed3\u6784\uff0c\u7136\u540e\u4f7f\u7528 orm \u5e93\u6839\u636e\u8be5\u7ed3\u6784\u751f\u6210\u6a21\u578b\u7c7b\u3002\u8fd9\u79cd\u65b9\u6cd5\u7701\u53bb\u4e86\u5728 python \u4e2d\u624b\u52a8\u7f16\u5199\u6a21\u578b\u7c7b\u7684\u9700\u8981\u3002<\/p>\n<p><strong>django orm<\/strong><\/p>\n<p>django orm \u540c\u65f6\u652f\u6301\u67b6\u6784\u4f18\u5148\u548c\u4ee3\u7801\u4f18\u5148\u65b9\u6cd5\u3002\u8981\u4f7f\u7528\u67b6\u6784\u4f18\u5148\uff0c\u8bf7\u4f7f\u7528 inspectdb \u547d\u4ee4\u751f\u6210\u6a21\u578b\u7c7b\uff1a<\/p>\n<pre>python manage.py inspectdb &gt; models.py<\/pre>\n<p><strong>flask-sqlalchemy<\/strong><\/p>\n<p>flask-sqlalchemy \u662f flask \u6846\u67b6\u7684\u6d41\u884c orm\u3002\u5bf9\u4e8e\u67b6\u6784\u4f18\u5148\uff0c\u53ef\u4ee5\u4f7f\u7528 declarativemeta.metadata.create_all() \u65b9\u6cd5\u521b\u5efa\u8868\u548c\u6a21\u578b\u7c7b\uff1a<\/p>\n<pre>from flask_sqlalchemy import sqlalchemy\n\ndb = sqlalchemy()\n\nclass user:\n    ...\n\ndb.metadata.create_all()<\/pre>\n<p><strong>\u4ee3\u7801\u4f18\u5148\uff08codefirst\uff09\u65b9\u6cd5<\/strong><\/p>\n<p>\u5728\u4ee3\u7801\u4f18\u5148\u6216 codefirst \u65b9\u6cd5\u4e2d\uff0c\u60a8\u5148\u5728 python \u4ee3\u7801\u4e2d\u5b9a\u4e49\u6a21\u578b\u7c7b\uff0c\u7136\u540e orm \u5e93\u8d1f\u8d23\u521b\u5efa\u76f8\u5e94\u7684\u6570\u636e\u5e93\u8868\u3002\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u5728\u7f16\u5199\u4ee3\u7801\u65f6\u5b9a\u4e49\u6a21\u578b\uff0c\u800c\u4e0d\u662f\u4f9d\u8d56\u4e8e\u73b0\u6709\u8868\u7ed3\u6784\u3002<\/p>\n<p><strong>peewee<\/strong><\/p>\n<p>peewee orm \u652f\u6301\u4ee3\u7801\u4f18\u5148\u65b9\u6cd5\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 create_table \u65b9\u6cd5\u521b\u5efa\u8868\uff1a<\/p>\n<pre>from peewee import *\n\ndb = SqliteDatabase('database.db')\n\nclass User(Model):\n    ...\n\nUser.create_table(db)<\/pre>\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n<p>\u901a\u8fc7\u4f7f\u7528 codefirst \u6216 dbfirst \u65b9\u6cd5\u548c\u5408\u9002\u7684 orm \u5e93\uff0c\u60a8\u53ef\u4ee5\u907f\u514d\u7f16\u5199\u7e41\u7410\u7684\u6a21\u578b\u7c7b\uff0c\u540c\u65f6\u4ecd\u7136\u53d7\u76ca\u4e8e orm \u63d0\u4f9b\u7684\u4fbf\u5229\u6027\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528 Python ORM \u5e93\u6784\u5efa\u6570\u636e\u6a21\u578b\uff0c\u907f\u514d\u624b\u52a8\u7f16\u5199\u6a21\u578b\u7c7b\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\u5427\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 Python ORM &#46;&#46;&#46;<\/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-200161","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200161","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=200161"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200161\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}