{"id":194078,"date":"2020-11-01T17:58:15","date_gmt":"2020-11-01T09:58:15","guid":{"rendered":"http:\/\/4563.org\/?p=194078"},"modified":"2020-11-01T17:58:15","modified_gmt":"2020-11-01T09:58:15","slug":"django-orm-%e4%bd%bf%e7%94%a8-sql-%e5%87%bd%e6%95%b0%e6%80%8e%e4%b9%88%e5%86%99%e8%a1%a8%e8%be%be%e5%bc%8f%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=194078","title":{"rendered":"django ORM \u4f7f\u7528 sql \u51fd\u6570\u600e\u4e48\u5199\u8868\u8fbe\u5f0f\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  django ORM \u4f7f\u7528 sql \u51fd\u6570\u600e\u4e48\u5199\u8868\u8fbe\u5f0f\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wuwukai007 <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u6bd4\u5982<\/p>\n<p><strong>sql = select * from book where left(&#8216;name&#8217;,1)!=&#8217;z&#8217;<\/strong><\/p>\n<p>\u7528 orm \u6211\u76ee\u524d\u53ea\u80fd\u60f3\u5230<\/p>\n<pre><code>book.objects.extra(where=[\"left('name',1)!='z' \"]) <\/code><\/pre>\n<p>\u6216\u8005<\/p>\n<pre><code>from django.db.mdels.functions import Left  book.objects.annotate(name_left=Left('name',1)).filter(~Q(name_left='z')) <\/code><\/pre>\n<p>\u7b2c\u4e00\u79cd\u8981\u5199\u539f\u751f sql\uff0c\u7b2c\u4e8c\u79cd\u4e0d\u5199\u539f\u751f sql\uff0c\u4f46\u662f \u767d\u767d\u52a0\u4e86\u4e00\u4e2a name_left \u5b57\u6bb5\uff0c \u6709\u6ca1\u6709\u8fd9\u79cd\u4f7f\u7528\u4e86 sql \u51fd\u6570\uff0c\u8fd8\u80fd\u505a\u5224\u65ad\u7684 \uff0c\u901a\u8fc7 ORM \u5199\u51fa\u6765\u7684<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>6<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4199836\" 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>                                                             \u5bf9\u4e8e\u4f60\u8fd9\u4e2a\u60c5\u5f62\uff0c\u7528 book.objects.exclude(name__startswith=&#8217;z&#8217;)\u5c31\u53ef\u4ee5\u5b9e\u73b0                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4199837\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : wuwukai007 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @nonduality left \u53ea\u662f\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5b9e\u9645\u60c5\u51b5\u91cc\u9762\u53ef\u80fd\u7528\u522b\u7684\u51fd\u6570 instr \u7b49\uff0c\u53ea\u662f\u60f3\u63a2\u8ba8\u4e0b \u5bf9\u4e8e\u8fd9\u79cd \u5e26\u51fd\u6570\u7684\uff0c\u80fd\u4e0d\u80fd \u5199\u51fa\u8868\u8fbe\u5f0f\uff0c!= ,= \u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4199838\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : freakxx <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Custom Lookups<br \/>https:\/\/docs.djangoproject.com\/zh-hans\/3.1\/howto\/custom-lookups\/<\/p>\n<p>\u76f4\u63a5\u5b9a\u4e49\u4e2a\u65b0\u7684\u89c4\u5219\u5c31 ok                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4199839\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : wuwukai007 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @freakxx orm \u8fd8\u53ef\u4ee5\u81ea\u5b9a\u4e49\u89c4\u5219\uff0c\u770b\u6765\u8fd8\u662f\u6587\u6863\u770b\u7684\u4e0d\u591f\u7ec6\uff0c\u591a\u8c22                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4199840\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : freakxx <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @wuwukai007 #4 <\/p>\n<p>\u8fd9\u4e2a\u5b9e\u73b0\u8fd8\u662f\u633a\u9a9a\u6c14\u7684\uff0c<\/p>\n<p>\u4f60\u63d0\u7684\u70b9\u6211\u4e5f\u521a\u624d\u4e5f\u53bb\u770b\u80fd\u4e0d\u80fd\u5b9e\u73b0\uff0c<br \/>\u5b9e\u9645\u4e0a\u53ef\u4ee5\u60f3\u529e\u6cd5\u505a\u6210 substrwith \u8fd9\u6837\u7684\u4e1c\u897f\uff0c\u4f46\u5b98\u65b9\u597d\u50cf\u4e0d\u652f\u6301\u52a8\u6001\u540d\u5b57\uff0c<br \/>\u6bd4\u5982 substrwith__&lt;int&gt; \u6765\u52a8\u6001\u6539\u53d8\uff0c<\/p>\n<p>\u6240\u4ee5\u4f60\u53ef\u4ee5\u8003\u8651\u5148\u505a\u4e2a\u6bd4\u8f83\u7b80\u5355\u7684\uff0c\u6bd4\u5982 stat \u5440\uff0cend \u4e4b\u7c7b\u8fd8\u662f\u6bd4\u8f83\u7b80\u5355\u7684<\/p>\n<p>\u5173\u4e8e django\uff0cdrf \u7684\uff0c\u6587\u6863\u548c\u6e90\u7801\u4e4b\u524d\u4e5f\u90fd\u64b8\u8fc7\uff0c<br \/>\u5982\u679c\u4e00\u822c\u63d0\u95ee\u6ca1\u601d\u8def\uff0c\u4e5f\u53ef\u4ee5 at \u6211\uff0c\u6211\u8fd8\u662f\u633a\u559c\u6b22\u5404\u79cd pythonic \u7684\u5b9e\u73b0                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4199841\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : freakxx <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5bf9\u5e94\u6e90\u7801\u53ef\u4ee5\u53bb<br \/>django\/db\/models\/lookups.py<\/p>\n<p>https:\/\/github.com\/django\/django\/blob\/master\/django\/db\/models\/lookups.py<\/p>\n<p>\u770b\u770b\uff0c\u4f9d\u74e2\u753b\u846b\u82a6\u8fd8\u662f\u633a\u7b80\u5355\u7684\u4e8b\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>django ORM \u4f7f\u7528 sql&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\/194078"}],"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=194078"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/194078\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=194078"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=194078"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=194078"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}