{"id":317023,"date":"2021-02-04T20:21:25","date_gmt":"2021-02-04T12:21:25","guid":{"rendered":"http:\/\/4563.org\/?p=317023"},"modified":"2021-02-04T20:21:25","modified_gmt":"2021-02-04T12:21:25","slug":"django-%e5%a6%82%e4%bd%95%e4%bc%98%e5%8c%96%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=317023","title":{"rendered":"django \u5982\u4f55\u4f18\u5316\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  django \u5982\u4f55\u4f18\u5316\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lixuda <\/span>  <span><i><\/i> 7<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5148\u8bf4\u8bf4\u4f7f\u7528\u611f\u89c9\uff1a \u5c0f\u767d\u7528\u6237\uff0c\u4e4b\u524d\u7528 flask\uff0c\u540e\u9762\u53d1\u73b0\u9700\u8981\u592a\u591a\u81ea\u5df1\u5b9e\u73b0\u529f\u80fd\uff0c\u6539\u6210 django\uff0c\u5f00\u53d1\u529f\u80fd\u901f\u5ea6\u9a6c\u4e0a\u63d0\u5347\u3002 \u6ca1\u6709\u6570\u636e\u5bf9\u6bd4\uff0c\u53ea\u80fd\u4f7f\u7528\u7684\u611f\u89c9 flask \u6bd4 django \u54cd\u5e94\u5feb\u70b9\uff0c\u53ea\u80fd\u662f\u4e00\u79cd\u611f\u89c9\u3002 \u53e6\u5916\u5076\u7136\u6027\u51fa\u73b0\u8bfb\u53d6\u6570\u636e\u5e93\u65f6\u95f4\u8fc7\u957f\uff0c\u53cd\u590d\u6d4b\u8bd5\uff0c\u5c31\u662f\u5076\u53d1\u6027\u3002<\/p>\n<p>\u6240\u4ee5\u95ee\u95ee\u9ad8\u624b\u4eec\uff0c\u5177\u4f53\u4f7f\u7528\u8fc7\u7a0b\u4e2d django \u8be5\u5982\u4f55\u4f18\u5316\uff1f<\/p>\n<ol>\n<li>value value_list \u65b9\u5f0f\u80fd\u5426\u52a0\u5feb\u6570\u636e\u8bfb\u53d6\uff1f \u5982:Model.objects.values_list(&#8216;key&#8217;,&#8217;key1&#8242;).filter(id=id).all()[:10]<\/li>\n<li>\u5173\u95ed USE_I18N \u80fd\u6709\u591a\u5c11\u63d0\u5347?<\/li>\n<li>ORM \u4f7f\u7528\u4e0a\u6709\u4ec0\u4e48\u597d\u7684\u6280\u5de7?<\/li>\n<li>\u54ea\u4e9b\u914d\u7f6e\u53ef\u4ee5\u5173\u95ed\u5982 MIDDLEWARE<\/li>\n<li>\u6709\u6ca1\u6709\u597d\u7684\u76ee\u5f55\u7ed3\u6784\u63a8\u8350<\/li>\n<li>\u6709\u6ca1\u6709\u66f4\u591a\u4f18\u5316?<\/li>\n<\/ol><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>13<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5005164\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wuwukai007 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u628a csrf \u5173\u4e86\uff0c\u901f\u5ea6\u7acb\u9a6c\u63d0\u5347                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5005165\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : XiaoxiaoPu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             ORM \u67e5\u8be2\u4f18\u5316\u53ef\u4ee5\u770b\u770b www.jianshu.com\/p\/aded5b2029f6<br \/>\u4e0d\u8fc7\u7b2c\u4e00\u6b65\u662f\u5148\u7cbe\u786e\u5b9a\u4f4d\u5230\u6162\u5728\u54ea                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5005166\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : charexcalibur <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7a7a\u4e86\u53ef\u4ee5\u770b\u770b\u5b98\u65b9\u7684\u4f18\u5316\u5efa\u8bae https:\/\/docs.djangoproject.com\/en\/3.1\/topics\/performance\/                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5005167\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : reorx <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             1. \u53ef\u4ee5\uff0cvalues \/ values_list \u76f8\u5f53\u4e8e\u53ea select \u4e86\u6307\u5b9a\u7684 fields \uff1b\u5982\u679c\u9700\u8981\u5f97\u5230 model instance \u53ef\u4ee5\u7528 only \/ defer<br \/>2. \u521d\u6b21\u542f\u52a8\u4ee3\u7801\u52a0\u8f7d\u7684\u63d0\u5347\uff0c\u4e0d\u5f71\u54cd view \u7684\u5904\u7406\u65f6\u95f4<br \/>4. \u4e86\u89e3\u6bcf\u4e2a\u5177\u4f53\u7684\u4f5c\u7528\uff0c\u89c6\u60c5\u51b5\u5173\u95ed\uff0c\u6781\u7aef\u60c5\u51b5\u4e0b\u90fd\u53ef\u4ee5\u5173\u95ed<br \/>5. \u53ef\u4ee5\u770b\u4e0b\u6211\u81ea\u5df1\u5e38\u7528\u7684\u811a\u624b\u67b6\u9879\u76ee\uff0c\u6682\u65f6\u8fd8\u6ca1\u6709\u6587\u6863\u8bf4\u660e\uff0c\u4e0e django \u5b98\u65b9\u6559\u7a0b\u7684\u4e0d\u540c\u5728\u4e8e\uff0c\u53ea\u6709\u4e00\u4e2a app\uff0c\u4f46\u4f1a\u6709\u591a\u4e2a modules \u4e2d\u5b58\u5728 models\uff0c\u901a\u8fc7\u81ea\u5b9a\u4e49 AppConfig \u5b8c\u6210\u52a0\u8f7d https:\/\/github.com\/reorx\/django_starter_pack\/tree\/master\/starter_app                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5005168\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : lixuda <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @reorx \u611f\u8c22,https:\/\/github.com\/reorx\/django_starter_pack\/tree\/master\/starter_app 404?                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5005169\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zachlhb <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             use_i18n \u4e0d\u5efa\u8bae\u5173\u95ed\uff0c\u961f\u5217\u6846\u67b6\u7528\u7684\u662f utc \u65f6\u95f4\u7684\uff0c\u5173\u4e86\u961f\u5217\u7684\u65f6\u95f4\u5c31\u4f1a\u6709\u5927\u5751                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5005170\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : reorx <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @lixuda \u4e0d\u597d\u610f\u601d\uff0c\u5fd8\u4e86\u6539\u9879\u76ee\u7684\u53ef\u89c1\u6027\uff0c\u73b0\u5728\u597d\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5005171\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : allisone <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             django \u5c31\u662f\u4e0a\u624b\u5feb                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5005172\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : lixuda <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @reorx \u53ef\u4ee5\u4e86\uff0c\u975e\u5e38\u611f\u8c22\u3002\u53e6\u5916\u770b\u89c1\u53ef\u4ee5\u5916\u90e8\u7a0b\u5e8f\u5f15\u7528 django model \u7684 orm \uff1f\u4e4b\u524d\u5b9a\u65f6\u7a0b\u5e8f\uff0c\u7528 peewee \u6765\u505a\uff0c\u5982\u679c\u80fd\u76f4\u63a5\u7528 app \u91cc\u9762 model \u5c31\u65b9\u4fbf\u591a\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5005173\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : reorx <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @lixuda \u53ef\u4ee5\u5728\u5916\u90e8\u7a0b\u5e8f\u4e2d\u5f15\u7528 django orm\uff0c\u9700\u8981\u5728 import models \u4e4b\u524d\u5b8c\u6210 `django.setup()`, \u6211\u5c01\u88c5\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570 `setup_django` \u4e13\u95e8\u7528\u6765\u6267\u884c\u8fd9\u4e2a\u64cd\u4f5c\uff0c\u89c1\u6587\u4ef6 `utils\/django_setup.py`.<\/p>\n<p>\u793a\u4f8b\u89c1 `cli.py`: https:\/\/github.com\/reorx\/django_starter_pack\/blob\/master\/cli.py                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5005174\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : erakayin <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u53ef\u4ee5\u8bd5\u4e0b https:\/\/github.com\/jazzband\/django-silk                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5005175\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : nonduality <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5b98\u7f51\u7684\u6559\u7a0b\u5f88\u8be6\u7ec6\u4e86\uff0c\u6309\u7167\u90a3\u4e2a\u53bb\u505a\u901f\u5ea6\u5c31\u80fd\u63d0\u5347\u5f88\u591a\uff0c\u57fa\u672c\u4e0a\u662f\uff1a<br \/>0 \u3001\u9996\u5148\u7528 django debug tools \u68c0\u67e5\u5230\u5e95\u6162\u5728\u54ea\uff0c\u8fd9\u4e2a\u5de5\u5177\u8fd8\u662f\u633a\u597d\u7528\u7684\uff1b\u5176\u6b21\u6253\u5f00 context_processors \u91cc\u5934\u7684 debug\uff0c\u518d\u6253\u5f00\u65e5\u5fd7 django \u91cc\u5934\u7684 log_level \u4e3a debug\uff0c\u8fd9\u6837\u6570\u636e\u5e93\u6bcf\u6b21\u8bbf\u95ee\u7684 sql \u8bed\u53e5\u548c\u6b21\u6570\u90fd\u4f1a\u6253\u5370\u51fa\u6765\uff0c\u65b9\u4fbf\u8fdb\u884c\u4f18\u5316\uff1b<br \/>1 \u3001\u5bf9\u6570\u636e\u5e93\u4f7f\u7528 prefetch\uff0c\u7528 only \u6216 defer \u83b7\u53d6\u53ea\u9700\u8981\u7684\u5b57\u6bb5\uff0c\u6570\u636e\u5e93\u5199\u5165\u53ef\u4ee5\u4f7f\u7528 update_fields \u53ea\u66f4\u65b0\u7279\u5b9a\u5b57\u6bb5\uff1b values_list \u662f\u80fd\u63d0\u5347\u8bfb\u53d6\u901f\u5ea6\uff0c\u4f46\u7528\u8d77\u6765\u4e0d\u7b97\u592a\u65b9\u4fbf\uff1b<br \/>2 \u3001\u5bf9\u7f51\u9875\u7247\u6bb5\u6216\u751f\u6210\u7684 json \u7f13\u5b58\uff08\u914d\u5408 Redis \u6216 DiskCache \u505a\u540e\u7aef\u90fd\u884c\uff09\uff1b<br \/>3 \u3001\u53d6\u6d88\u4e0d\u5fc5\u8981\u7684 middleware\uff0c\u6bd4\u5982\u4e0d\u9700\u8981\u5728\u6a21\u677f\u7528 Message\uff0c\u53ef\u4ee5\u628a\u5b83\u53d6\u6d88\uff1b\u8fd8\u6709 context_processors \u91cc\u5934 request \u8ddf auth \u662f\u91cd\u590d\u7684\uff0c\u4ec5\u65b9\u4fbf\u4f60\u7528 request \u5bf9\u8c61\uff0c\u4f60\u4e0d\u9700\u8981\u53ef\u4ee5\u53d6\u6d88 request\uff0c\u9700\u8981\u65f6\u518d\u4f20\u5165\u6a21\u677f\uff1b<br \/>4 \u3001\u751f\u6210 csrf key \u786e\u5b9e\u5f88\u6162\uff0c\u5982\u679c\u4e0d\u9700\u8981\u53ef\u4ee5\u5173\u6389\uff1b<br \/>5 \u3001\u9488\u5bf9\u7279\u5b9a view\uff0c\u5bf9\u5176\u7b97\u6cd5\u8fdb\u884c\u4f18\u5316\uff0c\u6bd4\u65b9\u6211\u6709\u9700\u8981\u9891\u7e41\u5408\u6210\u4e00\u4e2a\u7279\u5b9a\u65f6\u95f4\uff0c\u540e\u6765\u4f18\u5316\u6210\u7528\u65f6\u95f4\u6233\u53bb\u7b97\uff0c\u5c31\u5feb\u4e86\u5f88\u591a\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5005176\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : tmackan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5148\u4f18\u5316\u4f60\u7684\u4e1a\u52a1\u903b\u8f91 \/\u4e1a\u52a1\u6d41\u7a0b<br \/>profile \u627e\u5230\u8017\u65f6\u7684 func\uff0c\u7136\u540e\u60f3\u529e\u6cd5\u4f18\u5316\u6389<br \/>\u53e6\u5916 ORM \u5fc5\u987b\u6253\u5370\u51fa SQL \u8bed\u53e5\u8fd8\u8981\u81ea\u5df1\u5206\u6790\uff0c\u662f\u5426\u5408\u7406<br \/>py \u7684 orm \u4e2d\u9ed8\u8ba4\u5927\u91cf\u662f\u7528 select * \u7684\u67e5\u8be2\uff0c\u65e0\u6cd5\u8d70\u7d22\u5f15\u8986\u76d6\uff0c\u9700\u8981\u56de\u8868\u3002\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>django \u5982\u4f55\u4f18\u5316\uff1f \u8cc7\u6df1\u5927\u4f6c&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\/317023"}],"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=317023"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/317023\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=317023"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=317023"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=317023"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}