{"id":6749,"date":"2024-11-14T14:04:23","date_gmt":"2024-11-14T06:04:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6749\/"},"modified":"2024-11-14T14:04:23","modified_gmt":"2024-11-14T06:04: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:\/\/fwq.ai\/blog\/6749\/","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><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/172985995278744.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 Python ORM \u5e93\u6784\u5efa\u6570\u636e\u6a21\u578b\uff0c\u907f\u514d\u624b\u52a8\u7f16\u5199\u6a21\u578b\u7c7b\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 Python ORM \u5e93\u6784\u5efa\u6570\u636e\u6a21\u578b\uff0c\u907f\u514d\u624b\u52a8\u7f16\u5199\u6a21\u578b\u7c7b\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u65e0\u9700\u7f16\u5199\u6a21\u578b\u7c7b\u7684  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><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528 Python ORM \u5e93\u6784\u5efa\u6570\u636e\u6a21\u578b\uff0c\u907f\u514d\u624b\u52a8\u7f16\u5199\u6a21\u578b\u7c7b\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u65e0\u9700\u7f16\u5199\u6a21\u578b\u7c7b\u7684 orm \u89e3\u51b3\u65b9\u6848 \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 \u67b6\u6784\u4f18\u5148\uff08dbfirst\uff09\u65b9\u6cd5 \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 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b django orm 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 python manage.py inspectdb &gt; models.py \u767b\u5f55\u540e\u590d\u5236 flask-sqlalchemy 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 from flask_sqlalchemy import sqlalchemy db = sqlalchemy() class user: &#8230; db.metadata.create_all() \u767b\u5f55\u540e\u590d\u5236 \u4ee3\u7801\u4f18\u5148\uff08codefirst\uff09\u65b9\u6cd5 \u5728\u4ee3\u7801\u4f18\u5148\u6216 codefirst \u65b9\u6cd5\u4e2d\uff0c\u60a8\u5148\u5728 python [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-6749","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6749","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=6749"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6749\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6749"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}