{"id":2980,"date":"2016-11-24T19:46:22","date_gmt":"2016-11-24T11:46:22","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2980"},"modified":"2016-11-24T19:46:22","modified_gmt":"2016-11-24T11:46:22","slug":"django%e7%9a%84orm%e4%bd%bf%e7%94%a8%e8%ae%b0%e5%bd%95_2","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2980.html","title":{"rendered":"Django\u7684ORM\u4f7f\u7528\u8bb0\u5f55_2"},"content":{"rendered":"<p>=Start=<\/p>\n<h4>\u7f18\u7531\uff1a<\/h4>\n<p>\u5728\u4f7f\u7528\u7684\u8fc7\u7a0b\u4e2d\u8bb0\u5f55\u4e00\u4e0bDjango\u7684ORM\u77e5\u8bc6\/\u6280\u5de7\uff0c\u4e00\u6765\u53ef\u4ee5\u52a0\u6df1\u5370\u8c61\uff0c\u540c\u65f6\u4e5f\u4e3a\u65b9\u4fbf\u4ee5\u540e\u53c2\u8003\u3002<\/p>\n<h4>\u6b63\u6587\uff1a<\/h4>\n<h5>1.Django\u4e2d\u7684\u6bd4\u8f83\u64cd\u4f5c<\/h5>\n<p style=\"padding-left: 30px;\">exact<br \/>\niexact<br \/>\ncontains<br \/>\nicontains<br \/>\nin<br \/>\ngt<br \/>\ngte<br \/>\nlt<br \/>\nlte<br \/>\nstartswith<br \/>\nistartswith<br \/>\nendswith<br \/>\niendswith<br \/>\nrange<br \/>\nyear<br \/>\nmonth<br \/>\nday<br \/>\nweek_day<br \/>\nisnull<br \/>\nsearch<br \/>\nregex<br \/>\niregex<\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/687295\/how-do-i-do-a-not-equal-in-django-queryset-filtering\/1155013#1155013\">http:\/\/stackoverflow.com\/questions\/687295\/how-do-i-do-a-not-equal-in-django-queryset-filtering\/1155013#1155013<\/a><\/li>\n<li><a href=\"https:\/\/my.oschina.net\/u\/993130\/blog\/209460\">https:\/\/my.oschina.net\/u\/993130\/blog\/209460<\/a><\/li>\n<\/ul>\n<h5>2.\u5728Django\u7684ORM\u4e2d\u5982\u4f55\u7528\u65e5\u671f\u533a\u95f4\u8fdb\u884c\u8fc7\u6ee4\uff1f<\/h5>\n<pre class=\"lang:default decode:true\"># \u6709\u70b9\u95ee\u9898\nSample.objects.filter(date__range=[\"2011-01-01\", \"2011-01-31\"])\n\n# \u4e5f\u6709\u70b9\u95ee\u9898\nimport datetime\nsamples = Sample.objects.filter(sampledate__gt=datetime.date(2011, 1, 1),\n                                sampledate__lt=datetime.date(2011, 1, 31))\n\n# \u53ef\u4ee5\uff08start\/end\u7684\u683c\u5f0f\u4e3a\u300c2011-01-01\u300d\uff0cdate_int\u4e3a\u6574\u578b\u5b57\u6bb5\uff09\nSample.objects.filter(date_int__gte=start.replace('-', ''), date_int__lte=end.replace('-', ''))<\/pre>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/4668619\/django-database-query-how-to-filter-objects-by-date-range\/4668703#4668703\">http:\/\/stackoverflow.com\/questions\/4668619\/django-database-query-how-to-filter-objects-by-date-range\/4668703#4668703<\/a><\/p>\n<h5>3.Django\u7684ORM\u4e2d\u5982\u4f55\u8fdb\u884c\u591a\u6761\u4ef6\u67e5\u8be2\uff1f<\/h5>\n<pre class=\"lang:default decode:true \">#\u53ef\u4ee5\u5728Python\u7a0b\u5e8f\u4e2d\u7528key\/value\u683c\u5f0f\u7684\u53c2\u6570\u586b\u5145\u4e0b\u9762\u7684\u300cname\u300d\u548c\u300cA\/Z\u300d\nkwargs = {\n    '{0}__{1}'.format('name', 'startswith'): 'A',\n    '{0}__{1}'.format('name', 'endswith'): 'Z'\n}\n\nPerson.objects.filter(**kwargs)<\/pre>\n<p>QuerySet\u662f\u5ef6\u8fdf\u7684\uff0c\u521b\u5efaQuerySet\u4e0d\u4f1a\u89e6\u53ca\u5230\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u4f60\u53ef\u4ee5\u591a\u4e2a\u8fc7\u6ee4\u5408\u5e76\u5230\u4e00\u8d77\uff0c\u76f4\u5230\u6c42\u503c\u7684\u65f6\u5019Django\u624d\u4f1a\u5f00\u59cb\u67e5\u8be2\u3002<\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"https:\/\/docs.djangoproject.com\/en\/1.8\/ref\/models\/querysets\/#django.db.models.query.QuerySet.filter\">https:\/\/docs.djangoproject.com\/en\/1.8\/ref\/models\/querysets\/#django.db.models.query.QuerySet.filter<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/310732\/in-django-how-does-one-filter-a-queryset-with-dynamic-field-lookups\">http:\/\/stackoverflow.com\/questions\/310732\/in-django-how-does-one-filter-a-queryset-with-dynamic-field-lookups<\/a>\u00a0#\u7ecf\u5178<\/li>\n<\/ul>\n<h5>4.Django\u7684ORM\u4e2dfilter(\u6761\u4ef61).filter(\u6761\u4ef62)\u4e0efilter(\u6761\u4ef61,\u6761\u4ef62)\u7684\u533a\u522b<\/h5>\n<p>filter(\u6761\u4ef61).filter(\u6761\u4ef62) \u4e0e filter(\u6761\u4ef61,\u6761\u4ef62) \u90fd\u8868\u793a\u7684\u662f\u300c<span style=\"color: #ff0000;\"><strong>\u540c\u65f6\u6ee1\u8db3\u6761\u4ef61\u548c\u6761\u4ef62<\/strong><\/span>\u300d\uff0c\u4f46\u662f\u540e\u8005\u4f1a\u6bd4\u524d\u8005\u7684\u6548\u7387\u7a0d\u9ad8\u4e00\u70b9\u70b9(\u5dee\u522b\u4e5f\u4e0d\u5927\uff0c\u770b\u4e2a\u4eba\u559c\u597d)\u3002<\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/plq168.blog.163.com\/blog\/static\/531014622010428111225872\/\">http:\/\/plq168.blog.163.com\/blog\/static\/531014622010428111225872\/<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/8164675\/chaining-multiple-filter-in-django-is-this-a-bug\">http:\/\/stackoverflow.com\/questions\/8164675\/chaining-multiple-filter-in-django-is-this-a-bug<\/a><\/li>\n<li><a href=\"https:\/\/docs.djangoproject.com\/en\/1.8\/topics\/db\/queries\/#spanning-multi-valued-relationships\">https:\/\/docs.djangoproject.com\/en\/1.8\/topics\/db\/queries\/#spanning-multi-valued-relationships<\/a><\/li>\n<\/ul>\n<h5>5.\u5728Django\u7684ORM\u4e2d\u5982\u4f55\u4f7f\u7528\u591a\u5217\u4f5c\u4e3a\u4e3b\u952e(\u590d\u5408\u4e3b\u952e)\uff1f<\/h5>\n<pre class=\"lang:default decode:true \">class MyTable(models.Model):\n    ...\n    class Meta:\n        unique_together = (('key1', 'key2'),)<\/pre>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"https:\/\/code.djangoproject.com\/wiki\/MultipleColumnPrimaryKeys\">https:\/\/code.djangoproject.com\/wiki\/MultipleColumnPrimaryKeys<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/16800375\/how-can-set-two-field-primary-key-for-my-models-in-django\">http:\/\/stackoverflow.com\/questions\/16800375\/how-can-set-two-field-primary-key-for-my-models-in-django<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/28712848\/composite-primary-key-in-django\">http:\/\/stackoverflow.com\/questions\/28712848\/composite-primary-key-in-django<\/a><\/li>\n<\/ul>\n<h5>6.\u5728Django\u7684ORM\u4e2d\u4f7f\u7528 Q \u5bf9\u8c61\u6784\u5efa\u590d\u6742\u7684\u67e5\u8be2\u8bed\u53e5<\/h5>\n<ul>\n<li><a href=\"https:\/\/mozillazg.com\/2015\/11\/django-the-power-of-q-objects-and-how-to-use-q-object.html\">https:\/\/mozillazg.com\/2015\/11\/django-the-power-of-q-objects-and-how-to-use-q-object.html<\/a><\/li>\n<\/ul>\n<h5>7.\u5176\u5b83<\/h5>\n<h6>\u4e3b\u952e\u5bfc\u81f4\u7684\u76f8\u5173\u95ee\u9898<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/28917252\/error-in-a-class-when-trying-to-syncdb-python-django\">http:\/\/stackoverflow.com\/questions\/28917252\/error-in-a-class-when-trying-to-syncdb-python-django<\/a><\/li>\n<li><a href=\"https:\/\/docs.djangoproject.com\/en\/1.10\/ref\/checks\/#models\">https:\/\/docs.djangoproject.com\/en\/1.10\/ref\/checks\/#models<\/a><br \/>\n<a href=\"http:\/\/blog.csdn.net\/zdxkzgy\/article\/details\/49951107\">http:\/\/blog.csdn.net\/zdxkzgy\/article\/details\/49951107<\/a><\/li>\n<\/ul>\n<h6>Django ORM\u603b\u7ed3<\/h6>\n<ul>\n<li><a href=\"http:\/\/www.cnblogs.com\/linjiqin\/archive\/2014\/07\/01\/3817954.html\">http:\/\/www.cnblogs.com\/linjiqin\/archive\/2014\/07\/01\/3817954.html<\/a><\/li>\n<\/ul>\n<h6>\u6570\u636e\u5e93\u8bbe\u8ba1\u4e09\u5927\u8303\u5f0f<\/h6>\n<ul>\n<li><a href=\"http:\/\/www.cnblogs.com\/linjiqin\/archive\/2012\/04\/01\/2428695.html\">http:\/\/www.cnblogs.com\/linjiqin\/archive\/2012\/04\/01\/2428695.html<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u5728\u4f7f\u7528\u7684\u8fc7\u7a0b\u4e2d\u8bb0\u5f55\u4e00\u4e0bDjango\u7684ORM\u77e5\u8bc6\/\u6280\u5de7\uff0c\u4e00\u6765\u53ef\u4ee5\u52a0\u6df1\u5370\u8c61\uff0c\u540c\u65f6\u4e5f\u4e3a\u65b9\u4fbf\u4ee5 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,23,7],"tags":[627,681,8],"class_list":["post-2980","post","type-post","status-publish","format-standard","hentry","category-database","category-knowledgebase-2","category-programing","tag-django","tag-orm","tag-python"],"views":3038,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2980","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=2980"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2980\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}