{"id":2993,"date":"2016-11-30T20:11:53","date_gmt":"2016-11-30T12:11:53","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2993"},"modified":"2016-11-30T20:11:53","modified_gmt":"2016-11-30T12:11:53","slug":"django%e7%9a%84orm%e4%bd%bf%e7%94%a8%e8%ae%b0%e5%bd%95_3","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2993.html","title":{"rendered":"Django\u7684ORM\u4f7f\u7528\u8bb0\u5f55_3"},"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.\u5728Django\u7684ORM\u4e2d\u5982\u4f55\u8bbf\u95ee\u5b58\u50a8\u8fc7\u7a0b\uff1f<\/h5>\n<pre class=\"lang:default decode:true\">from django.db import connection\ncursor = connection.cursor()\ncursor.execute(\"SQL STATEMENT CAN BE ANYTHING\")\n#cursor.fetchone()\ncursor.fetchall()<\/pre>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/805393\/what-is-the-best-way-to-access-stored-procedures-in-djangos-orm\">http:\/\/stackoverflow.com\/questions\/805393\/what-is-the-best-way-to-access-stored-procedures-in-djangos-orm<\/a><\/li>\n<li><a href=\"https:\/\/docs.djangoproject.com\/en\/1.8\/topics\/db\/sql\/#performing-raw-queries\">https:\/\/docs.djangoproject.com\/en\/1.8\/topics\/db\/sql\/#performing-raw-queries<\/a><\/li>\n<\/ul>\n<h5>2.\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\u5668<\/h5>\n<ul>\n<li><a href=\"http:\/\/fullstackpython.atjiang.com\/object-relational-mappers-orms.html\">http:\/\/fullstackpython.atjiang.com\/object-relational-mappers-orms.html<\/a><\/li>\n<li><a href=\"https:\/\/www.fullstackpython.com\/object-relational-mappers-orms.html\">https:\/\/www.fullstackpython.com\/object-relational-mappers-orms.html<\/a><\/li>\n<\/ul>\n<h5>3.\u4e3a\u4ec0\u4e48Django\u7684ORM\u6bd4\u539f\u751f\u7684MySQLdb\u6162\uff1f<\/h5>\n<p>\u5728\u5927\u67e5\u8be2\u4e0b\uff0c\u6bd4\u5982\u62a5\u8868\u4e1a\u52a1\uff0c\u4e0d\u5efa\u8bae\u4f7f\u7528ORM\uff0c\u56e0\u4e3a\u901f\u5ea6\u6bd4\u8f83\u6162\uff0c\u539f\u56e0\u5728\u4e8e\uff1a\u5b83\u4f1a\u628a\u7ed3\u679c\u8f6c\u6362\u6210QuerySet\u7ed3\u6784\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u8fd4\u56de\uff1b\u6240\u4ee5\u8fd9\u65f6\u53ef\u4ee5\u8003\u8651\u7528\u539f\u751f\u7684SQL\u8bed\u53e5\u3002<\/p>\n<p>\u5728Python\u4e0b\u53ef\u4ee5\u9009\u62e9 MySQLdb \uff0c\u4e5f\u53ef\u4ee5\u7528Django\u7684connection\/connections\u3002\u63a8\u8350\u7528connection\/connections\uff0c\u56e0\u4e3a\u6570\u636e\u5e93\u914d\u7f6e\u7b49\u4fe1\u606f\u90fd\u5df2\u7ecf\u653e\u5728\u4e86settings.py\u91cc\u9762\u3002<\/p>\n<pre class=\"lang:default decode:true\">from django.db import connection, transaction\nimport MySQLdb\n# ...\ncursor = connection.cursor(cursorclass = MySQLdb.cursors.DictCursor)\ncursor.execute(sql, None)\n\n# \u5982\u679c\u4f60\u6709\u591a\u4e2a\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u5728connections\u4e2d\u9009\u62e9\u6570\u636e\u5e93\uff1a\nfrom django.db import connections\ncursor = connections['crazyof'].cursor()\n\ntransaction.commit_unless_managed(using='crazyof')<\/pre>\n<ul>\n<li><a href=\"http:\/\/xiaorui.cc\/2015\/09\/24\/%E8%AF%9D%E8%AF%B4django-orm%E6%A8%A1%E5%9E%8B%E4%B8%BA%E4%BB%80%E4%B9%88%E6%AF%94%E5%8E%9F%E7%94%9F%E7%9A%84mysqldb%E6%85%A2\/\">\u8bdd\u8bf4Django orm\u6027\u80fd\u4e3a\u4ec0\u4e48\u6bd4\u539f\u751f\u7684mysqldb\u6162<\/a><\/li>\n<\/ul>\n<h5>4.\u83b7\u53d6Django ORM query\u6267\u884c\u7684\u7684SQL\u8bed\u53e5<\/h5>\n<pre class=\"lang:default decode:true\"># \u65b9\u6cd5\u4e00\nqueryset = MyModel.objects.all()\nprint 'Query SQL: {0}'.format(queryset.query)\n\n# \u65b9\u6cd5\u4e8c(debug=True)\nfrom django.db import connections\nprint connections['default'].queries\n\n# \u65b9\u6cd5\u4e09\n# \u5229\u7528\u4e09\u65b9\u63d0\u4f9b\u7684 middleware \u6765\u5b9e\u73b0\uff0c\u53c2\u8003\uff1a https:\/\/djangosnippets.org\/snippets\/290\/\n# \u662f\u63d2\u62d4\u5f0f\u7684\uff0c\u4e0d\u9700\u8981\u7684\u65f6\u5019\u5728 settings.py \u4e2d\u53bb\u6389\u8fd9\u4e2amiddleware\u5c31\u53ef\u4ee5\u4e86\uff0c\u4e0d\u8fc7\u8fd9\u53ea\u80fd\u5728debug \u6a21\u5f0f\u4e0b\u4f7f\u7528<\/pre>\n<ul>\n<li><a href=\"http:\/\/www.yihaomen.com\/article\/python\/448.htm\">http:\/\/www.yihaomen.com\/article\/python\/448.htm<\/a><\/li>\n<\/ul>\n<h5>5.\u5728Django\u7684ORM\u4e2d\u5982\u4f55\u5224\u65ad\u67e5\u8be2\u7ed3\u679c\u662f\u5426\u4e3a\u7a7a<\/h5>\n<pre class=\"lang:default decode:true\">my_queryset = Booking.objects.filter(date_select='2011-12-2')\n\n#\u65b9\u6cd5\u4e00 .exists()\nif my_queryset.exists():\n    print \"QuerySet has Data\"\nelse:\n    print \"QuerySet is empty\"\n#\u65b9\u6cd5\u4e8c .count()==0\nif my_queryset.count() == 0:\n    print \"empty\"\n#\u65b9\u6cd5\u4e09\nif my_queryset:\n    print \"QuerySet has Data\"\nelse:\n    print \"QuerySet is empty\"\n\n\u603b\u7ed3\uff1a\nQuerySet.exists() &gt; QuerySet.count()==0 &gt; QuerySet<\/pre>\n<ul>\n<li><a href=\"https:\/\/docs.djangoproject.com\/en\/1.8\/topics\/db\/optimization\/#use-queryset-count\">https:\/\/docs.djangoproject.com\/en\/1.8\/topics\/db\/optimization\/#use-queryset-count<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/8365255\/how-to-specify-empty-queryset-to-not-empty-queryset-to-object-hi\">http:\/\/stackoverflow.com\/questions\/8365255\/how-to-specify-empty-queryset-to-not-empty-queryset-to-object-hi<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/9669853\/how-can-i-test-for-an-empty-queryset-in-django\">http:\/\/stackoverflow.com\/questions\/9669853\/how-can-i-test-for-an-empty-queryset-in-django<\/a><\/li>\n<li><a href=\"https:\/\/docs.djangoproject.com\/en\/1.8\/ref\/models\/querysets\/#django.db.models.query.QuerySet.all\">https:\/\/docs.djangoproject.com\/en\/1.8\/ref\/models\/querysets\/#django.db.models.query.QuerySet.all<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/1387727\/checking-for-empty-queryset-in-django\">http:\/\/stackoverflow.com\/questions\/1387727\/checking-for-empty-queryset-in-django<\/a><\/li>\n<\/ul>\n<h5>6.\u5728Python\u7684MySQLdb\u6a21\u5757\u4e2d\u5982\u4f55\u5224\u65ad\u67e5\u8be2\u7ed3\u679c\u662f\u5426\u4e3a\u7a7a<\/h5>\n<pre class=\"lang:default decode:true\">cursor = connections['security'].cursor()\nif sql_params:\n    cursor.execute(sql_str, sql_params)\nelse:\n    cursor.execute(sql_str)\n\nif cursor.rowcount:\n    aaData = []\n    for item in cursor:\n        aaData.append(list(item))\n    return JsonResponse({'aaData': aaData})\nelse:\n    return JsonResponse({'aaData': []})<\/pre>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/16561362\/python-how-to-check-if-a-result-set-is-empty\">http:\/\/stackoverflow.com\/questions\/16561362\/python-how-to-check-if-a-result-set-is-empty<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/19382396\/print-if-mysql-returns-no-results\">http:\/\/stackoverflow.com\/questions\/19382396\/print-if-mysql-returns-no-results<\/a><\/li>\n<\/ul>\n<h5>7.\u5f85\u6dfb\u52a0<\/h5>\n<p>\u2026\u2026<\/p>\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,16,681],"class_list":["post-2993","post","type-post","status-publish","format-standard","hentry","category-database","category-knowledgebase-2","category-programing","tag-django","tag-mysql","tag-orm"],"views":6153,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2993","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=2993"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2993\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}