{"id":730,"date":"2021-10-27T08:48:04","date_gmt":"2021-10-26T23:48:04","guid":{"rendered":"https:\/\/vasyworks.yworks.net\/?page_id=730"},"modified":"2021-10-27T08:49:36","modified_gmt":"2021-10-26T23:49:36","slug":"explanation_django_raw_sql","status":"publish","type":"page","link":"https:\/\/vasyworks.yworks.net\/index.php\/explanation\/explanation_django_raw_sql\/","title":{"rendered":"Django\u3067\u306eSQL\u306e\u76f4\u63a5\u5b9f\u884c\u306b\u3064\u3044\u3066\uff1aVasyworks\u89e3\u8aac"},"content":{"rendered":"\n<p>Django\u3067\u306f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u64cd\u4f5c\u306fO\/R\u30de\u30c3\u30d1\u30fc\u3092\u5229\u7528\u3059\u308b\u306e\u304c\u4e00\u822c\u7684\u3067\u3059\u3002\u3067\u304d\u308b\u3060\u3051O\/R\u30de\u30c3\u30d1\u30fc\u3092\u5229\u7528\u3057\u305f\u65b9\u304c\u3044\u3044\u306e\u3067\u3059\u304c\u3001\u6761\u4ef6\u306e\u8907\u96d1\u306a\u30af\u30a8\u30ea\u3084\u96c6\u8a08\u30af\u30a8\u30ea\u306a\u3069\u3092\u5b9f\u884c\u3057\u305f\u3044\u5834\u5408\u306b\u3069\u3046\u3057\u3066\u3082\u76f4\u63a5SQL\u3092\u8a18\u8ff0\u3057\u3066\u5b9f\u884c\u3055\u305b\u305f\u3044\u3053\u3068\u3082\u3042\u308a\u307e\u3059\u3002Vasyworks\u3067\u3082\u8a73\u7d30\u306a\u6761\u4ef6\u3067\u306e\u7269\u4ef6\u691c\u7d22\u306a\u3069\u3001\u4e00\u90e8\u3067SQL\u6587\u3092\u76f4\u63a5\u8a18\u8ff0\u3057\u3066\u5b9f\u88c5\u3057\u3066\u3044\u307e\u3059\u3002\u4e0b\u8a18\u306b\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3092\u8a18\u8f09\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u25a0\u30b5\u30f3\u30d7\u30eb\u30e2\u30c7\u30eb\uff08models.py\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from django.db import models\n\nclass SampleModel(models.Model):\n    \"\"\"\n    \u30b5\u30f3\u30d7\u30eb\u30e2\u30c7\u30eb\n    \"\"\"\n    id = models.AutoField('ID', db_column='id', primary_key=True)\n    name = models.CharField('Name', db_column='name', max_length=100, db_index=True, null=True, blank=True)\n    product_code = models.CharField('Product code', db_column='product_code', max_length=5, db_index=True, null=True, blank=True)\n    price = models.IntegerField('Price', db_column='price', default=0)\n    \n    @classmethod\n    def get_raw_query_set(cls):\n    \"\"\"\n    RawQuerySet\u306e\u53d6\u5f97\n    \"\"\"\n    param = {}\n    \n    sql = 'SELECT id, name, product_code, price'\n    sql += ' FROM sample_table'\n    sql += ' WHERE product_code = %(product)s'\n    sql += ' AND price &gt;= %(money)s'\n    \n    param&#91;'product'] = 'A0001'\n    param&#91;'money'] = 1000\n    \n    return cls.objects.raw(raw_query=sql, params=params)\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8a18\u3067\u306f\u30e2\u30c7\u30eb\u306eSampleModel\u30af\u30e9\u30b9\u306eget_raw_query_set\u30e1\u30bd\u30c3\u30c9\u5185\u3067SQL\u6587\u3092\u8a18\u8ff0\u3057\u3066\u3001\u30e2\u30c7\u30eb\u30de\u30cd\u30fc\u30b8\u30e3\uff08objects\uff09\u306eraw\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3063\u3066\u30af\u30a8\u30ea\u30fc\u30bb\u30c3\u30c8\u3092\u53d6\u5f97\u3057\u3066\u3044\u307e\u3059\u3002raw\u30e1\u30bd\u30c3\u30c9\u306fRawQuerySet\u3068\u3044\u3046\u30af\u30e9\u30b9\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u8fd4\u3057\u307e\u3059\u3002RawQuerySet\u30af\u30e9\u30b9\u306f\u30e2\u30c7\u30eb\u30de\u30cd\u30fc\u30b8\u30e3\u306eall\u30e1\u30bd\u30c3\u30c9\u3084filter\u30e1\u30bd\u30c3\u30c9\u306a\u3069\u304c\u8fd4\u3059QuerySet\u30af\u30e9\u30b9\u3068\u4f3c\u3066\u3044\u307e\u3059\u304c\u3001RawQuerySet\u306b\u306fcount\u30e1\u30bd\u30c3\u30c9\u304c\u7121\u304flen\u95a2\u6570\u3067\u30ec\u30b3\u30fc\u30c9\u6570\u3092\u53d6\u3089\u306a\u3044\u3068\u3044\u3051\u306a\u3044\u306a\u3069\u3001\u82e5\u5e72\u6271\u3044\u304c\u7570\u306a\u308b\u3068\u3053\u308d\u304c\u3042\u308b\u3088\u3046\u3067\u3059\u3002\u3061\u306a\u307f\u306bDjango RESTFramework\u3067\u306fPagnation\u306e\u5185\u90e8\u3067count\u3092\u53d6\u308b\u969b\u306b\u3001count\u30e1\u30bd\u30c3\u30c9\u3067\u30a8\u30e9\u30fc\u304c\u51fa\u305f\u3089len\u95a2\u6570\u3067\u53d6\u5f97\u3059\u308b\u3088\u3046\u306b\u3057\u3066\u3001QuerySet\u3067\u3082RawQuerySet\u3067\u3082\u6271\u3048\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u5185\u3067Field\u3092\u8a18\u8ff0\u3059\u308b\u969b\u306edb_name\u3092SELECT\u6587\u306e\u30ab\u30e9\u30e0\u540d\u3068\u5408\u308f\u305b\u3066\u304a\u304f\u3068\u901a\u5e38\u306e\u30e2\u30c7\u30eb\u3068\u540c\u69d8\u306e\u30c7\u30fc\u30bf\u53c2\u7167\u304c\u53ef\u80fd\u3067\u3059\u3002SELECT\u6587\u306e\u30ab\u30e9\u30e0\u540d\u306bAS\u53e5\u3067\u30a8\u30a4\u30ea\u30a2\u30b9\u540d\u3092\u4ed8\u3051\u3066\u3001\u30a8\u30a4\u30ea\u30a2\u30b9\u540d\u3067Field\u306edb_name\u540d\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3082\u53ef\u80fd\u3067\u3059\u306e\u3067\u3001SQL\u306e\u96c6\u8a08\u95a2\u6570\u306e\u7d50\u679c\u306e\u53d6\u5f97\u306a\u3069\u306b\u3082\u5229\u7528\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u691c\u7d22\u6761\u4ef6\u306a\u3069\u3001\u5916\u90e8\u304b\u3089\u6307\u5b9a\u53ef\u80fd\u306a\u30d1\u30e9\u30e1\u30fc\u30bf\u3092SQL\u6587\u4e2d\u3067\u53d6\u308a\u6271\u3046\u969b\u306b\u306f\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u3002SQL\u6587\u4e2d\u306b\u8a18\u8ff0\u3059\u308b\u969b\u306bformat\u30e1\u30bd\u30c3\u30c9\u306a\u3069\u3067\u76f4\u63a5\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u66f8\u304d\u52a0\u3048\u3066\u3057\u307e\u3046\u3068SQL\u30a4\u30f3\u30b8\u30a7\u30af\u30b7\u30e7\u30f3\u767a\u751f\u306e\u539f\u56e0\u3068\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002 \u5916\u90e8\u304b\u3089\u6307\u5b9a\u53ef\u80fd\u306a\u30d1\u30e9\u30e1\u30fc\u30bf\u306fraw\u30e1\u30bd\u30c3\u30c9\u306e\u5f15\u6570\u306eparams\u3092\u4f7f\u3063\u3066\u6e21\u3059\u3053\u3068\u3067SQL\u30a4\u30f3\u30b8\u30a7\u30af\u30b7\u30e7\u30f3\u3092\u9632\u3050\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u691c\u7d22\u6761\u4ef6\u306a\u3069\u306e\u30d1\u30e9\u30e1\u30fc\u30bf \u306fSQL\u6587\u4e2d\u3067\u306f\u300c %(\u30d1\u30e9\u30e1\u30fc\u30bf\u540d)s\u300d\u3068\u3057\u3066\u8a18\u8ff0\u3057\u3001\u4e0e\u3048\u3089\u308c\u305f\u30d1\u30e9\u30e1\u30fc\u30bf\u306fSQL\u6587\u4e2d\u3067\u6307\u5b9a\u3057\u305f\u30d1\u30e9\u30e1\u30fc\u30bf\u540d\u4ed8\u304d\u306e\u9023\u60f3\u914d\u5217\u306b\u3057\u3066raw\u30e1\u30bd\u30c3\u30c9\u306eparams\u306b\u4e0e\u3048\u3066\u304f\u3060\u3055\u3044\u3002\u30d1\u30e9\u30e1\u30fc\u30bf\u540d\u7121\u3057\u306e\u300c%s\u300d\u3067\u6307\u5b9a\u3057\u3066\u30d1\u30e9\u30e1\u30fc\u30bf\u540d\u3092\u7701\u304f\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u304c\u3001\u30d1\u30e9\u30e1\u30fc\u30bf\u540d\u3092\u4f7f\u3063\u305f\u65b9\u304c\u591a\u6570\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u6307\u5b9a\u3059\u308b\u969b\u306b\u5206\u304b\u308a\u3084\u3059\u304f\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>Vasyworks\u3067\u306fVasyworksAPI\u306e\u7269\u4ef6\u691c\u7d22\u7528\uff08search\uff09API\u3084VasyworksSEARCH\u306e\u5730\u56f3\u691c\u7d22\u7528\u306eAPI\u3067\u76f4\u63a5SQL\u3092\u8a18\u8ff0\u3059\u308b\u5b9f\u88c5\u3092\u3057\u3066\u3044\u307e\u3059 \u3002\u691c\u7d22\u6761\u4ef6\u306e\u6307\u5b9a\u304c\u7d30\u304b\u304f\u3001O\/R\u30de\u30c3\u30d1\u30fc\u3067\u306f\u51e6\u7406\u901f\u5ea6\u304c\u843d\u3061\u3066\u3057\u307e\u3046\u306e\u3067\u3001\u305d\u306e\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u3002\u3054\u8208\u5473\u304c\u3042\u308b\u65b9\u306fGitHub\u3067\u516c\u958b\u3057\u3066\u3044\u308b\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Django\u3067\u306f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u64cd\u4f5c\u306fO\/R\u30de\u30c3\u30d1\u30fc\u3092\u5229\u7528\u3059\u308b\u306e\u304c\u4e00\u822c\u7684\u3067\u3059\u3002\u3067\u304d\u308b\u3060\u3051O\/R\u30de\u30c3\u30d1\u30fc\u3092\u5229\u7528\u3057\u305f\u65b9\u304c\u3044\u3044\u306e\u3067\u3059\u304c\u3001\u6761\u4ef6\u306e\u8907\u96d1\u306a\u30af\u30a8\u30ea\u3084\u96c6\u8a08\u30af\u30a8\u30ea\u306a\u3069\u3092\u5b9f\u884c\u3057\u305f\u3044\u5834\u5408\u306b\u3069\u3046\u3057\u3066\u3082\u76f4\u63a5SQL\u3092\u8a18\u8ff0\u3057\u3066\u5b9f\u884c\u3055\u305b\u305f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":567,"menu_order":4,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/vasyworks.yworks.net\/index.php\/wp-json\/wp\/v2\/pages\/730"}],"collection":[{"href":"https:\/\/vasyworks.yworks.net\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/vasyworks.yworks.net\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/vasyworks.yworks.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vasyworks.yworks.net\/index.php\/wp-json\/wp\/v2\/comments?post=730"}],"version-history":[{"count":13,"href":"https:\/\/vasyworks.yworks.net\/index.php\/wp-json\/wp\/v2\/pages\/730\/revisions"}],"predecessor-version":[{"id":746,"href":"https:\/\/vasyworks.yworks.net\/index.php\/wp-json\/wp\/v2\/pages\/730\/revisions\/746"}],"up":[{"embeddable":true,"href":"https:\/\/vasyworks.yworks.net\/index.php\/wp-json\/wp\/v2\/pages\/567"}],"wp:attachment":[{"href":"https:\/\/vasyworks.yworks.net\/index.php\/wp-json\/wp\/v2\/media?parent=730"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}