{"id":93727,"date":"2020-04-14T03:41:52","date_gmt":"2020-04-13T19:41:52","guid":{"rendered":"http:\/\/4563.org\/?p=93727"},"modified":"2020-04-14T03:41:52","modified_gmt":"2020-04-13T19:41:52","slug":"%e8%af%b7%e6%95%99%e5%85%b3%e4%ba%8e-django-%e7%9a%84-orm-%e8%af%ad%e5%8f%a5%e5%90%8c%e6%97%b6%e4%bf%ae%e6%94%b9%e5%a4%9a%e6%9d%a1%e6%95%b0%e6%8d%ae%e4%bb%a5%e5%8f%8a-flush-%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=93727","title":{"rendered":"\u8bf7\u6559\u5173\u4e8e django \u7684 orm \u8bed\u53e5\u540c\u65f6\u4fee\u6539\u591a\u6761\u6570\u636e\u4ee5\u53ca flush \u64cd\u4f5c"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u8bf7\u6559\u5173\u4e8e django \u7684 orm \u8bed\u53e5\u540c\u65f6\u4fee\u6539\u591a\u6761\u6570\u636e\u4ee5\u53ca flush \u64cd\u4f5c               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : kayseen <\/span>  <span><i><\/i> 20<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>tips: django \u4f7f\u7528\u7684 mysql, \u4ee5\u5f80\u7528\u7684\u662f flask \u7684 sqlalchemy, \u6bd4\u5982\u540c\u65f6\u4fee\u6539\u4e86\u4e24\u4e2a\u4e0d\u540c\u8868\u7684\u4e24\u6761\u6570\u636e user1 \u7684 name=1,class2 \u7684 name=class2,(\u4e24\u6761\u6570\u636e\u6ca1\u6709\u5173\u8054\u5173\u7cfb), \u6211\u53ef\u4ee5 model.add(user1),model.add(class2),\u6700\u540e model.commit()\u4e00\u6b21\u6027\u63d0\u4ea4,<\/p>\n<p>\u4f46\u662f\u5728 django \u7684 orm \u4e2d, \u5982\u679c\u6709\u4e0a\u8ff0\u7684\u573a\u666f, \u4e00\u6b21\u6027\u63d0\u4ea4\u4e24\u6761\u4fee\u6539\u7684\u6570\u636e(\u5f00\u4e8b\u7269?),\u8be5\u600e\u4e48\u5b9e\u73b0\u5462?<\/p>\n<p>\u627e\u5230\u4e86 django \u7684\u6279\u91cf\u521b\u5efa bulk_update(), \u4f46\u662f\u6b64\u65b9\u6cd5\u53ea\u652f\u6301\u6279\u91cf\u66f4\u65b0\u540c\u5f20\u8868\u7684\u591a\u6761\u6570\u636e,\u5e76\u4e14\u9700\u8981\u6307\u5b9a fields<\/p>\n<p>\u95ee\u9898: 1.\u6279\u91cf\u4fee\u6539\u591a\u5f20\u8868\u7684\u591a\u6761\u6570\u636e\u600e\u4e48\u4e00\u6b21\u6027\u63d0\u4ea4\u4fdd\u5b58? 2.django \u6709\u6ca1\u6709\u5728\u63d0\u4ea4\u6570\u636e\u4e4b\u524d\u7684 flush \u64cd\u4f5c(\u5728\u521b\u5efa\u65b0\u6570\u636e\u7684\u65f6\u5019,flush \u64cd\u4f5c\u53ef\u4ee5\u83b7\u53d6\u65b0\u6570\u636e\u7684 id,\u4f46\u662f\u6b64\u65f6\u6570\u636e\u5e76\u6ca1\u6709\u4fdd\u5b58\u5230 mysql \u4e2d)<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>5<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1165076\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : chen774553620 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             from django.db import transaction<br \/>with transaction.atomic:                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1165077\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : kayseen <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @chen774553620 <br \/>\u4e86\u89e3, \u771f\u68d2~                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1165078\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Vegetable <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/docs.djangoproject.com\/zh-hans\/2.2\/topics\/db\/transactions\/#why-django-uses-autocommit<br \/>\u4e2d\u6587\u6587\u6863\u4ed6\u4e0d\u9999\u5417                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1165079\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : kayseen <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Vegetable <br \/>\u8fd9\u4e2a\u4e1c\u897f\u770b\u8fc7\u5566\u54c8\u54c8, \u8bf7\u95ee\u60a8\u77e5\u9053\u5173\u4e8e django \u7684 flush \u64cd\u4f5c\u5417?                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1165080\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : EricCartmann <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e00\u5df2\u7ecf\u7ec8\u7ed3\u8fd9\u4e2a\u95ee\u9898\u3002transaction \u6a21\u5757\u5b8c\u7f8e\u89e3\u51b3\uff0c\u52a0\u4e0a with transaction.atomic()\u8fd8\u53ef\u4ee5\u63d0\u4f9b\u4e8b\u52a1\u7684\u7279\u6027                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf7\u6559\u5173\u4e8e django \u7684 orm&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/93727"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=93727"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/93727\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=93727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=93727"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=93727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}