{"title":"素の SQL 文の実行","version":"4.2","locale":"ja","docname":"topics/db/sql","url":"/ja/4.2/topics/db/sql/","canonical":"https://djangodocs.dev/ja/4.2/topics/db/sql/","summary":"Django gives you two ways of performing raw SQL queries: you can use Manager.raw() to perform raw queries and return model instances, or you can avoid the…","html":"<h1>素の SQL 文の実行<a class=\"heading-anchor\" href=\"#performing-raw-sql-queries\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Django gives you two ways of performing raw SQL queries: you can use\n<a class=\"reference internal\" href=\"#django.db.models.Manager.raw\" title=\"django.db.models.Manager.raw\"><code class=\"xref py py-meth docutils literal notranslate\">Manager.raw()</code></a> to <a class=\"reference internal\" href=\"#performing-raw-queries\">perform raw queries and return model instances</a>, or\nyou can avoid the model layer entirely and <a class=\"reference internal\" href=\"#executing-custom-sql-directly\">execute custom SQL directly</a>.</p>\n<aside class=\"admonition-explore-the-orm-before-using-raw-sql admonition\" role=\"note\">\n<p class=\"admonition-title\">Explore the ORM before using raw SQL!</p>\n<p>The Django ORM provides many tools to express queries without writing raw\nSQL. For example:</p>\n<ul class=\"simple\">\n<li><p>The <a class=\"reference internal\" href=\"/ja/4.2/ref/models/querysets/\"><span class=\"doc\">QuerySet API</span></a> is extensive.</p></li>\n<li><p>You can <a class=\"reference internal\" href=\"/ja/4.2/ref/models/querysets/#django.db.models.query.QuerySet.annotate\" title=\"django.db.models.query.QuerySet.annotate\"><code class=\"xref py py-meth docutils literal notranslate\">annotate</code></a> and <a class=\"reference internal\" href=\"/ja/4.2/topics/db/aggregation/\"><span class=\"doc\">aggregate</span></a> using many built-in <a class=\"reference internal\" href=\"/ja/4.2/ref/models/database-functions/\"><span class=\"doc\">database functions</span></a>. Beyond those, you can create\n<a class=\"reference internal\" href=\"/ja/4.2/ref/models/expressions/\"><span class=\"doc\">custom query expressions</span></a>.</p></li>\n</ul>\n<p>Before using raw SQL, explore <a class=\"reference internal\" href=\"/ja/4.2/topics/db/\"><span class=\"doc\">the ORM</span></a>. Ask on\none of <a class=\"reference internal\" href=\"/ja/4.2/faq/help/\"><span class=\"doc\">the support channels</span></a> to see if the ORM supports\nyour use case.</p>\n</aside>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">警告</p>\n<p>直接 SQL を書く場合はいかなる時も十分警戒するべきです。それを利用する時は毎回、利用者が <code class=\"docutils literal notranslate\">引数</code> を利用する事で任意に設定可能な全てのパラメータは SQL インジェクション攻撃から防御するため適切にエスケープすべきです。詳細は <a class=\"reference internal\" href=\"/ja/4.2/topics/security/#sql-injection-protection\"><span class=\"std std-ref\">SQL インジェクションの防御</span></a> を参照してください。</p>\n</aside>\n<section id=\"performing-raw-queries\">\n<span id=\"executing-raw-queries\"></span><h2>素のクエリを実行する<a class=\"heading-anchor\" href=\"#performing-raw-queries\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p><code class=\"docutils literal notranslate\">raw()</code> マネージャメソッドは素の SQL 文を処理してモデルのインスタンスを返させる場合に利用できます:</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Manager.raw\">\n<span class=\"sig-prename descclassname\">Manager.</span><span class=\"sig-name descname\">raw</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">raw_query</em>, <em class=\"sig-param\">params<span class=\"o\">=</span><span class=\"default_value\">()</span></em>, <em class=\"sig-param\">translations<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.Manager.raw\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>This method takes a raw SQL query, executes it, and returns a\n<code class=\"docutils literal notranslate\">django.db.models.query.RawQuerySet</code> instance. This <code class=\"docutils literal notranslate\">RawQuerySet</code> instance\ncan be iterated over like a normal <a class=\"reference internal\" href=\"/ja/4.2/ref/models/querysets/#django.db.models.query.QuerySet\" title=\"django.db.models.query.QuerySet\"><code class=\"xref py py-class docutils literal notranslate\">QuerySet</code></a> to\nprovide object instances.</p>\n<p>例を示す事で最も簡単に説明できます。以下のモデルについて考えてください:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">class</span> <span class=\"nc\">Person</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    first_name <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span><span class=\"o\">...</span><span class=\"p\">)</span>\n    last_name <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span><span class=\"o\">...</span><span class=\"p\">)</span>\n    birth_date <span class=\"o\">=</span> models<span class=\"o\">.</span>DateField<span class=\"p\">(</span><span class=\"o\">...</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>You could then execute custom SQL like so:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">for</span> p <span class=\"ow\">in</span> Person<span class=\"o\">.</span>objects<span class=\"o\">.</span>raw<span class=\"p\">(</span><span class=\"s2\">&quot;SELECT * FROM myapp_person&quot;</span><span class=\"p\">):</span>\n<span class=\"gp\">... </span>    <span class=\"nb\">print</span><span class=\"p\">(</span>p<span class=\"p\">)</span>\n<span class=\"gp\">...</span>\n<span class=\"go\">John Smith</span>\n<span class=\"go\">Jane Jones</span>\n</code></pre></div>\n<p>This example isn't very exciting -- it's exactly the same as running\n<code class=\"docutils literal notranslate\">Person.objects.all()</code>. However, <code class=\"docutils literal notranslate\">raw()</code> has a bunch of other options that\nmake it very powerful.</p>\n<aside class=\"admonition-model-table-names admonition\" role=\"note\">\n<p class=\"admonition-title\">モデルのテーブル名称</p>\n<p>この例で示したモデル <code class=\"docutils literal notranslate\">Person</code> のテーブル名はどのようにして得られたのでしょうか？</p>\n<p>デフォルトでは、Django はデータベース上のテーブル名をモデルの &quot;アプリのラベル名&quot; -- <code class=\"docutils literal notranslate\">manage.py startapp</code> で利用した名前 -- とモデルのクラス名とを、間にアンダースコアを入れて結合させる事で取得しています。先の例ではモデル <code class=\"docutils literal notranslate\">Person</code> が <code class=\"docutils literal notranslate\">myapp</code> と名づけられたアプリケーション内に作成されていると仮定すると、そのテーブル名が <code class=\"docutils literal notranslate\">myapp_person</code> であると推定できます。</p>\n<p>詳細に関しては、手動でデータベースのテーブル名を設定できる <a class=\"reference internal\" href=\"/ja/4.2/ref/models/options/#django.db.models.Options.db_table\" title=\"django.db.models.Options.db_table\"><code class=\"xref py py-attr docutils literal notranslate\">db_table</code></a> オプションのドキュメントを参照してください。</p>\n</aside>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">警告</p>\n<p><code class=\"docutils literal notranslate\">.raw()</code> に対して渡された SQL 文はチェックされません。Django はそこに記述された内容によってデータベースが行を返す事を期待しますが、それを強制する処理は行いません。もし記述したクエリが行を返さない場合、(おそらく不可解な)例外が発生します。</p>\n</aside>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">警告</p>\n<p>もしあなたが MySQL でクエリを処理する場合は、複数の型を扱う際に MySQL の暗黙的な型変換が予期しない結果をもたらす場合がある事に注意してください。もし文字列型で定義したカラムに対し、数値型の値で問い合わせた場合、MySQL は比較処理を行う前にテーブル上の全ての値の型を数値型に変換します。例えば <code class=\"docutils literal notranslate\">'abc'</code>、 <code class=\"docutils literal notranslate\">'def'</code> といった値が含まれているテーブルに対して <code class=\"docutils literal notranslate\">WHERE mycolumn=0</code> という条件での問い合わせを行うと、両方の行がマッチします。これを防ぐため、クエリの値を利用する前に適切な型キャストを行ってください。</p>\n</aside>\n<section id=\"mapping-query-fields-to-model-fields\">\n<h3>クエリのフィールドをモデルのフィールドにマップする<a class=\"heading-anchor\" href=\"#mapping-query-fields-to-model-fields\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p><code class=\"docutils literal notranslate\">raw()</code> は自動的にクエリのフィールドをモデルのフィールドにマップします。</p>\n<p>The order of fields in your query doesn't matter. In other words, both\nof the following queries work identically:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>Person<span class=\"o\">.</span>objects<span class=\"o\">.</span>raw<span class=\"p\">(</span><span class=\"s2\">&quot;SELECT id, first_name, last_name, birth_date FROM myapp_person&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>Person<span class=\"o\">.</span>objects<span class=\"o\">.</span>raw<span class=\"p\">(</span><span class=\"s2\">&quot;SELECT last_name, birth_date, first_name, id FROM myapp_person&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>Matching is done by name. This means that you can use SQL's <code class=\"docutils literal notranslate\">AS</code> clauses to\nmap fields in the query to model fields. So if you had some other table that\nhad <code class=\"docutils literal notranslate\">Person</code> data in it, you could easily map it into <code class=\"docutils literal notranslate\">Person</code> instances:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>Person<span class=\"o\">.</span>objects<span class=\"o\">.</span>raw<span class=\"p\">(</span>\n<span class=\"gp\">... </span>    <span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"gp\">... </span><span class=\"sd\">    SELECT first AS first_name,</span>\n<span class=\"gp\">... </span><span class=\"sd\">           last AS last_name,</span>\n<span class=\"gp\">... </span><span class=\"sd\">           bd AS birth_date,</span>\n<span class=\"gp\">... </span><span class=\"sd\">           pk AS id,</span>\n<span class=\"gp\">... </span><span class=\"sd\">    FROM some_other_table</span>\n<span class=\"gp\">... </span><span class=\"sd\">    &quot;&quot;&quot;</span>\n<span class=\"gp\">... </span><span class=\"p\">)</span>\n</code></pre></div>\n<p>名称が一致している限り、そのモデルのインスタンスは適切に生成されます。</p>\n<p>Alternatively, you can map fields in the query to model fields using the\n<code class=\"docutils literal notranslate\">translations</code> argument to <code class=\"docutils literal notranslate\">raw()</code>. This is a dictionary mapping names of\nfields in the query to names of fields on the model. For example, the above\nquery could also be written:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>name_map <span class=\"o\">=</span> <span class=\"p\">{</span><span class=\"s2\">&quot;first&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;first_name&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;last&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;last_name&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;bd&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;birth_date&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;pk&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;id&quot;</span><span class=\"p\">}</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>Person<span class=\"o\">.</span>objects<span class=\"o\">.</span>raw<span class=\"p\">(</span><span class=\"s2\">&quot;SELECT * FROM some_other_table&quot;</span><span class=\"p\">,</span> translations<span class=\"o\">=</span>name_map<span class=\"p\">)</span>\n</code></pre></div>\n</section>\n<section id=\"index-lookups\">\n<h3>インデックスの利用<a class=\"heading-anchor\" href=\"#index-lookups\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p><code class=\"docutils literal notranslate\">raw()</code> supports indexing, so if you need only the first result you can\nwrite:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>first_person <span class=\"o\">=</span> Person<span class=\"o\">.</span>objects<span class=\"o\">.</span>raw<span class=\"p\">(</span><span class=\"s2\">&quot;SELECT * FROM myapp_person&quot;</span><span class=\"p\">)[</span><span class=\"mi\">0</span><span class=\"p\">]</span>\n</code></pre></div>\n<p>However, the indexing and slicing are not performed at the database level. If\nyou have a large number of <code class=\"docutils literal notranslate\">Person</code> objects in your database, it is more\nefficient to limit the query at the SQL level:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>first_person <span class=\"o\">=</span> Person<span class=\"o\">.</span>objects<span class=\"o\">.</span>raw<span class=\"p\">(</span><span class=\"s2\">&quot;SELECT * FROM myapp_person LIMIT 1&quot;</span><span class=\"p\">)[</span><span class=\"mi\">0</span><span class=\"p\">]</span>\n</code></pre></div>\n</section>\n<section id=\"deferring-model-fields\">\n<h3>モデルのフィールドの遅延評価<a class=\"heading-anchor\" href=\"#deferring-model-fields\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Fields may also be left out:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>people <span class=\"o\">=</span> Person<span class=\"o\">.</span>objects<span class=\"o\">.</span>raw<span class=\"p\">(</span><span class=\"s2\">&quot;SELECT id, first_name FROM myapp_person&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>The <code class=\"docutils literal notranslate\">Person</code> objects returned by this query will be deferred model instances\n(see <a class=\"reference internal\" href=\"/ja/4.2/ref/models/querysets/#django.db.models.query.QuerySet.defer\" title=\"django.db.models.query.QuerySet.defer\"><code class=\"xref py py-meth docutils literal notranslate\">defer()</code></a>). This means that the\nfields that are omitted from the query will be loaded on demand. For example:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">for</span> p <span class=\"ow\">in</span> Person<span class=\"o\">.</span>objects<span class=\"o\">.</span>raw<span class=\"p\">(</span><span class=\"s2\">&quot;SELECT id, first_name FROM myapp_person&quot;</span><span class=\"p\">):</span>\n<span class=\"gp\">... </span>    <span class=\"nb\">print</span><span class=\"p\">(</span>\n<span class=\"gp\">... </span>        p<span class=\"o\">.</span>first_name<span class=\"p\">,</span>  <span class=\"c1\"># This will be retrieved by the original query</span>\n<span class=\"gp\">... </span>        p<span class=\"o\">.</span>last_name<span class=\"p\">,</span>  <span class=\"c1\"># This will be retrieved on demand</span>\n<span class=\"gp\">... </span>    <span class=\"p\">)</span>\n<span class=\"gp\">...</span>\n<span class=\"go\">John Smith</span>\n<span class=\"go\">Jane Jones</span>\n</code></pre></div>\n<p>From outward appearances, this looks like the query has retrieved both\nthe first name and last name. However, this example actually issued 3\nqueries. Only the first names were retrieved by the <code class=\"docutils literal notranslate\">raw()</code> query -- the\nlast names were both retrieved on demand when they were printed.</p>\n<p>There is only one field that you can't leave out - the primary key\nfield. Django uses the primary key to identify model instances, so it\nmust always be included in a raw query. A\n<a class=\"reference internal\" href=\"/ja/4.2/ref/exceptions/#django.core.exceptions.FieldDoesNotExist\" title=\"django.core.exceptions.FieldDoesNotExist\"><code class=\"xref py py-class docutils literal notranslate\">FieldDoesNotExist</code></a> exception will be raised if\nyou forget to include the primary key.</p>\n</section>\n<section id=\"adding-annotations\">\n<h3>付加情報の追加<a class=\"heading-anchor\" href=\"#adding-annotations\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>You can also execute queries containing fields that aren't defined on the\nmodel. For example, we could use <a class=\"reference external\" href=\"https://www.postgresql.org/docs/current/functions-datetime.html\">PostgreSQL's age() function</a> to get a list\nof people with their ages calculated by the database:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>people <span class=\"o\">=</span> Person<span class=\"o\">.</span>objects<span class=\"o\">.</span>raw<span class=\"p\">(</span><span class=\"s2\">&quot;SELECT *, age(birth_date) AS age FROM myapp_person&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">for</span> p <span class=\"ow\">in</span> people<span class=\"p\">:</span>\n<span class=\"gp\">... </span>    <span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"s2\">&quot;</span><span class=\"si\">%s</span><span class=\"s2\"> is </span><span class=\"si\">%s</span><span class=\"s2\">.&quot;</span> <span class=\"o\">%</span> <span class=\"p\">(</span>p<span class=\"o\">.</span>first_name<span class=\"p\">,</span> p<span class=\"o\">.</span>age<span class=\"p\">))</span>\n<span class=\"gp\">...</span>\n<span class=\"go\">John is 37.</span>\n<span class=\"go\">Jane is 42.</span>\n<span class=\"go\">...</span>\n</code></pre></div>\n<p>You can often avoid using raw SQL to compute annotations by instead using a\n<a class=\"reference internal\" href=\"/ja/4.2/ref/models/expressions/#func-expressions\"><span class=\"std std-ref\">Func() expression</span></a>.</p>\n</section>\n<section id=\"passing-parameters-into-raw\">\n<h3><code class=\"docutils literal notranslate\">raw()</code> にパラメータを渡す<a class=\"heading-anchor\" href=\"#passing-parameters-into-raw\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If you need to perform parameterized queries, you can use the <code class=\"docutils literal notranslate\">params</code>\nargument to <code class=\"docutils literal notranslate\">raw()</code>:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>lname <span class=\"o\">=</span> <span class=\"s2\">&quot;Doe&quot;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>Person<span class=\"o\">.</span>objects<span class=\"o\">.</span>raw<span class=\"p\">(</span><span class=\"s2\">&quot;SELECT * FROM myapp_person WHERE last_name = </span><span class=\"si\">%s</span><span class=\"s2\">&quot;</span><span class=\"p\">,</span> <span class=\"p\">[</span>lname<span class=\"p\">])</span>\n</code></pre></div>\n<p><code class=\"docutils literal notranslate\">params</code> is a list or dictionary of parameters. You'll use <code class=\"docutils literal notranslate\">%s</code>\nplaceholders in the query string for a list, or <code class=\"docutils literal notranslate\">%(key)s</code>\nplaceholders for a dictionary (where <code class=\"docutils literal notranslate\">key</code> is replaced by a\ndictionary key), regardless of your database engine. Such placeholders will be\nreplaced with parameters from the <code class=\"docutils literal notranslate\">params</code> argument.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">注釈</p>\n<p>SQLite バックエンドにおいて辞書はサポートされていません; パラメータはリストで渡す必要が有ります。</p>\n</aside>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">警告</p>\n<p><strong>Do not use string formatting on raw queries or quote placeholders in your\nSQL strings!</strong></p>\n<p>It's tempting to write the above query as:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>query <span class=\"o\">=</span> <span class=\"s2\">&quot;SELECT * FROM myapp_person WHERE last_name = </span><span class=\"si\">%s</span><span class=\"s2\">&quot;</span> <span class=\"o\">%</span> lname\n<span class=\"gp\">&gt;&gt;&gt; </span>Person<span class=\"o\">.</span>objects<span class=\"o\">.</span>raw<span class=\"p\">(</span>query<span class=\"p\">)</span>\n</code></pre></div>\n<p>You might also think you should write your query like this (with quotes\naround <code class=\"docutils literal notranslate\">%s</code>):</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>query <span class=\"o\">=</span> <span class=\"s2\">&quot;SELECT * FROM myapp_person WHERE last_name = &#39;</span><span class=\"si\">%s</span><span class=\"s2\">&#39;&quot;</span>\n</code></pre></div>\n<p><strong>Don't make either of these mistakes.</strong></p>\n<p>As discussed in <a class=\"reference internal\" href=\"/ja/4.2/topics/security/#sql-injection-protection\"><span class=\"std std-ref\">SQL injectionへの防御</span></a>, using the <code class=\"docutils literal notranslate\">params</code>\nargument and leaving the placeholders unquoted protects you from <a class=\"reference external\" href=\"https://en.wikipedia.org/wiki/SQL_injection\">SQL\ninjection attacks</a>, a common exploit where attackers inject arbitrary\nSQL into your database. If you use string interpolation or quote the\nplaceholder, you're at risk for SQL injection.</p>\n</aside>\n</section>\n</section>\n<section id=\"executing-custom-sql-directly\">\n<span id=\"executing-custom-sql\"></span><h2>独自の SQL を直接実行する<a class=\"heading-anchor\" href=\"#executing-custom-sql-directly\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p><a class=\"reference internal\" href=\"#django.db.models.Manager.raw\" title=\"django.db.models.Manager.raw\"><code class=\"xref py py-meth docutils literal notranslate\">Manager.raw()</code></a> でも要求を満たせない場合があります:きれいにモデルにマップできないクエリを扱ったり、<code class=\"docutils literal notranslate\">UPDATE</code>、 <code class=\"docutils literal notranslate\">INSERT</code> あるいは <code class=\"docutils literal notranslate\">DELETE</code> を直接実行したりする必要が有るかもしれません。</p>\n<p>こういったケースでは、モデル層を完全に迂回してデータベースにいつでも直接アクセスできます。</p>\n<p><code class=\"docutils literal notranslate\">django.db.connection</code> オブジェクトがデフォルトのデータベース接続に対応しています。そのデータベース接続を利用するには、カーソルオブジェクトを取得するため <code class=\"docutils literal notranslate\">connection.cursor()</code> を呼び出してください。続いて、<code class=\"docutils literal notranslate\">cursor.execute(sql, [params])</code> を呼び出して SQL を実行した後 <code class=\"docutils literal notranslate\">cursor.fetchone()</code> もしくは <code class=\"docutils literal notranslate\">cursor.fetchall()</code> で結果の行を取得します。</p>\n<p>例:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> connection\n\n\n<span class=\"k\">def</span> <span class=\"nf\">my_custom_sql</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n    <span class=\"k\">with</span> connection<span class=\"o\">.</span>cursor<span class=\"p\">()</span> <span class=\"k\">as</span> cursor<span class=\"p\">:</span>\n        cursor<span class=\"o\">.</span>execute<span class=\"p\">(</span><span class=\"s2\">&quot;UPDATE bar SET foo = 1 WHERE baz = </span><span class=\"si\">%s</span><span class=\"s2\">&quot;</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"bp\">self</span><span class=\"o\">.</span>baz<span class=\"p\">])</span>\n        cursor<span class=\"o\">.</span>execute<span class=\"p\">(</span><span class=\"s2\">&quot;SELECT foo FROM bar WHERE baz = </span><span class=\"si\">%s</span><span class=\"s2\">&quot;</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"bp\">self</span><span class=\"o\">.</span>baz<span class=\"p\">])</span>\n        row <span class=\"o\">=</span> cursor<span class=\"o\">.</span>fetchone<span class=\"p\">()</span>\n\n    <span class=\"k\">return</span> row\n</code></pre></div>\n<p>To protect against SQL injection, you must not include quotes around the <code class=\"docutils literal notranslate\">%s</code>\nplaceholders in the SQL string.</p>\n<p>もしリテラルなパーセント記号をクエリ中で使いたい場合、パラメータを渡す際に二重に記述する必要が有る事に注意してください:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>cursor<span class=\"o\">.</span>execute<span class=\"p\">(</span><span class=\"s2\">&quot;SELECT foo FROM bar WHERE baz = &#39;30%&#39;&quot;</span><span class=\"p\">)</span>\ncursor<span class=\"o\">.</span>execute<span class=\"p\">(</span><span class=\"s2\">&quot;SELECT foo FROM bar WHERE baz = &#39;30</span><span class=\"si\">%%</span><span class=\"s2\">&#39; AND id = </span><span class=\"si\">%s</span><span class=\"s2\">&quot;</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"bp\">self</span><span class=\"o\">.</span>id<span class=\"p\">])</span>\n</code></pre></div>\n<p><a class=\"reference internal\" href=\"/ja/4.2/topics/db/multi-db/\"><span class=\"doc\">2 つ以上のデータベース</span></a> を利用している場合、特定の接続(とカーソル)を取得するのに <code class=\"docutils literal notranslate\">django.db.connections</code> を利用できます。<code class=\"docutils literal notranslate\">django.db.connections</code> はその別名を指定する事で特定の接続を得られる、辞書に似たオブジェクトです:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> connections\n\n<span class=\"k\">with</span> connections<span class=\"p\">[</span><span class=\"s2\">&quot;my_db_alias&quot;</span><span class=\"p\">]</span><span class=\"o\">.</span>cursor<span class=\"p\">()</span> <span class=\"k\">as</span> cursor<span class=\"p\">:</span>\n    <span class=\"c1\"># Your code here</span>\n    <span class=\"o\">...</span>\n</code></pre></div>\n<p>デフォルトでは、Python データベース API は返す結果にフィールド名を含まず、つまり <code class=\"docutils literal notranslate\">辞書</code> でなく、 <code class=\"docutils literal notranslate\">リスト</code> の値として結果を返します。処理能力とメモリを少々利用して、次のような処理を用いる事で結果を <code class=\"docutils literal notranslate\">辞書</code> として得られます:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">def</span> <span class=\"nf\">dictfetchall</span><span class=\"p\">(</span>cursor<span class=\"p\">):</span>\n    <span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">    Return all rows from a cursor as a dict.</span>\n<span class=\"sd\">    Assume the column names are unique.</span>\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n    columns <span class=\"o\">=</span> <span class=\"p\">[</span>col<span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span> <span class=\"k\">for</span> col <span class=\"ow\">in</span> cursor<span class=\"o\">.</span>description<span class=\"p\">]</span>\n    <span class=\"k\">return</span> <span class=\"p\">[</span><span class=\"nb\">dict</span><span class=\"p\">(</span><span class=\"nb\">zip</span><span class=\"p\">(</span>columns<span class=\"p\">,</span> row<span class=\"p\">))</span> <span class=\"k\">for</span> row <span class=\"ow\">in</span> cursor<span class=\"o\">.</span>fetchall<span class=\"p\">()]</span>\n</code></pre></div>\n<p>別の選択肢は Python 標準ライブラリに含まれる <a class=\"reference external\" href=\"https://docs.python.org/3/library/collections.html#collections.namedtuple\" title=\"(in Python v3.14)\"><code class=\"xref py py-func docutils literal notranslate\">collections.namedtuple()</code></a> を利用する事です。<code class=\"docutils literal notranslate\">namedtuple</code> は属性を指定して値へのアクセスが可能なタプルに似たオブジェクトです; 加えてインデックスが利用でき、イテレート可能でもあります。取得した結果は不変であり属性名もしくはインデックスでアクセスできて便利です:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">collections</span> <span class=\"kn\">import</span> namedtuple\n\n\n<span class=\"k\">def</span> <span class=\"nf\">namedtuplefetchall</span><span class=\"p\">(</span>cursor<span class=\"p\">):</span>\n    <span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">    Return all rows from a cursor as a namedtuple.</span>\n<span class=\"sd\">    Assume the column names are unique.</span>\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n    desc <span class=\"o\">=</span> cursor<span class=\"o\">.</span>description\n    nt_result <span class=\"o\">=</span> namedtuple<span class=\"p\">(</span><span class=\"s2\">&quot;Result&quot;</span><span class=\"p\">,</span> <span class=\"p\">[</span>col<span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span> <span class=\"k\">for</span> col <span class=\"ow\">in</span> desc<span class=\"p\">])</span>\n    <span class=\"k\">return</span> <span class=\"p\">[</span>nt_result<span class=\"p\">(</span><span class=\"o\">*</span>row<span class=\"p\">)</span> <span class=\"k\">for</span> row <span class=\"ow\">in</span> cursor<span class=\"o\">.</span>fetchall<span class=\"p\">()]</span>\n</code></pre></div>\n<p>The <code class=\"docutils literal notranslate\">dictfetchall()</code> and <code class=\"docutils literal notranslate\">namedtuplefetchall()</code> examples assume unique\ncolumn names, since a cursor cannot distinguish columns from different tables.</p>\n<p>Here is an example of the difference between the three:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>cursor<span class=\"o\">.</span>execute<span class=\"p\">(</span><span class=\"s2\">&quot;SELECT id, parent_id FROM test LIMIT 2&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>cursor<span class=\"o\">.</span>fetchall<span class=\"p\">()</span>\n<span class=\"go\">((54360982, None), (54360880, None))</span>\n\n<span class=\"gp\">&gt;&gt;&gt; </span>cursor<span class=\"o\">.</span>execute<span class=\"p\">(</span><span class=\"s2\">&quot;SELECT id, parent_id FROM test LIMIT 2&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>dictfetchall<span class=\"p\">(</span>cursor<span class=\"p\">)</span>\n<span class=\"go\">[{&#39;parent_id&#39;: None, &#39;id&#39;: 54360982}, {&#39;parent_id&#39;: None, &#39;id&#39;: 54360880}]</span>\n\n<span class=\"gp\">&gt;&gt;&gt; </span>cursor<span class=\"o\">.</span>execute<span class=\"p\">(</span><span class=\"s2\">&quot;SELECT id, parent_id FROM test LIMIT 2&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>results <span class=\"o\">=</span> namedtuplefetchall<span class=\"p\">(</span>cursor<span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>results\n<span class=\"go\">[Result(id=54360982, parent_id=None), Result(id=54360880, parent_id=None)]</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>results<span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span><span class=\"o\">.</span>id\n<span class=\"go\">54360982</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>results<span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">][</span><span class=\"mi\">0</span><span class=\"p\">]</span>\n<span class=\"go\">54360982</span>\n</code></pre></div>\n<section id=\"connections-and-cursors\">\n<h3>データベース接続とカーソル<a class=\"heading-anchor\" href=\"#connections-and-cursors\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p><code class=\"docutils literal notranslate\">connection</code> と <code class=\"docutils literal notranslate\">cursor</code> の実装は <span class=\"target\" id=\"index-2\"></span><a class=\"pep reference external\" href=\"https://peps.python.org/pep-0249/\"><strong>PEP 249</strong></a> に規定された Python DB-API の基準にほぼ完全に準拠しています — <a class=\"reference internal\" href=\"/ja/4.2/topics/db/transactions/\"><span class=\"doc\">トランザクション操作</span></a> は例外となります。</p>\n<p>Python DB-API に精通していない場合、<code class=\"docutils literal notranslate\">cursor.execute()</code> 内の SQL 文には直接値を追加せずに、<code class=\"docutils literal notranslate\">&quot;%s&quot;</code> というプレースホルダーを利用する事に注意してください。この手法を用いる事で、内部で動作しているデータベースライブラリは自動的に必要に応じて値のエスケープを行ってくれます。</p>\n<p>加えて Django はプレースホルダーとして Python に内蔵された SQLite モジュールで用いられる <code class=\"docutils literal notranslate\">&quot;?&quot;</code> でなく <code class=\"docutils literal notranslate\">&quot;%s&quot;</code> を期待して動作する事にも注意してください。これは全体の調和と健全性のためです。</p>\n<p>コンテキストマネージャとしてのカーソルの利用:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">with</span> connection<span class=\"o\">.</span>cursor<span class=\"p\">()</span> <span class=\"k\">as</span> c<span class=\"p\">:</span>\n    c<span class=\"o\">.</span>execute<span class=\"p\">(</span><span class=\"o\">...</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>これは以下と同じです:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>c <span class=\"o\">=</span> connection<span class=\"o\">.</span>cursor<span class=\"p\">()</span>\n<span class=\"k\">try</span><span class=\"p\">:</span>\n    c<span class=\"o\">.</span>execute<span class=\"p\">(</span><span class=\"o\">...</span><span class=\"p\">)</span>\n<span class=\"k\">finally</span><span class=\"p\">:</span>\n    c<span class=\"o\">.</span>close<span class=\"p\">()</span>\n</code></pre></div>\n<section id=\"calling-stored-procedures\">\n<h4>Calling stored procedures<a class=\"heading-anchor\" href=\"#calling-stored-procedures\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.CursorWrapper.callproc\">\n<span class=\"sig-prename descclassname\">CursorWrapper.</span><span class=\"sig-name descname\">callproc</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">procname</em>, <em class=\"sig-param\">params<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">kparams<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.CursorWrapper.callproc\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Calls a database stored procedure with the given name. A sequence\n(<code class=\"docutils literal notranslate\">params</code>) or dictionary (<code class=\"docutils literal notranslate\">kparams</code>) of input parameters may be\nprovided. Most databases don't support <code class=\"docutils literal notranslate\">kparams</code>. Of Django's built-in\nbackends, only Oracle supports it.</p>\n<p>For example, given this stored procedure in an Oracle database:</p>\n<div class=\"code-block\" data-language=\"sql\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">SQL</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"SQL code\"><code><span class=\"k\">CREATE</span> <span class=\"k\">PROCEDURE</span> <span class=\"ss\">&quot;TEST_PROCEDURE&quot;</span><span class=\"p\">(</span>v_i <span class=\"nb\">INTEGER</span><span class=\"p\">,</span> v_text NVARCHAR2<span class=\"p\">(</span><span class=\"mi\">10</span><span class=\"p\">))</span> <span class=\"k\">AS</span>\n    p_i <span class=\"nb\">INTEGER</span><span class=\"p\">;</span>\n    p_text NVARCHAR2<span class=\"p\">(</span><span class=\"mi\">10</span><span class=\"p\">);</span>\n<span class=\"k\">BEGIN</span>\n    p_i <span class=\"p\">:</span><span class=\"o\">=</span> v_i<span class=\"p\">;</span>\n    p_text <span class=\"p\">:</span><span class=\"o\">=</span> v_text<span class=\"p\">;</span>\n    <span class=\"p\">...</span>\n<span class=\"k\">END</span><span class=\"p\">;</span>\n</code></pre></div>\n<p>This will call it:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">with</span> connection<span class=\"o\">.</span>cursor<span class=\"p\">()</span> <span class=\"k\">as</span> cursor<span class=\"p\">:</span>\n    cursor<span class=\"o\">.</span>callproc<span class=\"p\">(</span><span class=\"s2\">&quot;test_procedure&quot;</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">,</span> <span class=\"s2\">&quot;test&quot;</span><span class=\"p\">])</span>\n</code></pre></div>\n</dd></dl>\n\n</section>\n</section>\n</section>","rootId":"performing-raw-sql-queries","toc":[{"title":"素のクエリを実行する","anchor":"performing-raw-queries","children":[{"title":"クエリのフィールドをモデルのフィールドにマップする","anchor":"mapping-query-fields-to-model-fields","children":[]},{"title":"インデックスの利用","anchor":"index-lookups","children":[]},{"title":"モデルのフィールドの遅延評価","anchor":"deferring-model-fields","children":[]},{"title":"付加情報の追加","anchor":"adding-annotations","children":[]},{"title":"raw() にパラメータを渡す","anchor":"passing-parameters-into-raw","children":[]}]},{"title":"独自の SQL を直接実行する","anchor":"executing-custom-sql-directly","children":[{"title":"データベース接続とカーソル","anchor":"connections-and-cursors","children":[{"title":"Calling stored procedures","anchor":"calling-stored-procedures","children":[]}]}]}],"breadcrumbs":[{"docname":"topics/index","title":"Django を使う","url":"/ja/4.2/topics/"},{"docname":"topics/db/index","title":"モデルとデータベース","url":"/ja/4.2/topics/db/"}],"prev":{"docname":"topics/db/managers","title":"マネージャ","url":"/ja/4.2/topics/db/managers/"},"next":{"docname":"topics/db/transactions","title":"データベースのトランザクション","url":"/ja/4.2/topics/db/transactions/"},"formats":{"html":"/ja/4.2/topics/db/sql/","markdown":"/ja/4.2/topics/db/sql.md","json":"/ja/4.2/topics/db/sql.json"},"source":"https://github.com/django/django/blob/stable/4.2.x/docs/topics/db/sql.txt","official":"https://docs.djangoproject.com/ja/4.2/topics/db/sql/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}