... はこのAPIに従う例です。あるクラスが以下のメソッドを実装している場合、そのクラスはクエリ式APIに従っていると言えます: as_sql ( compiler , connection ) 式の SQL フラグメントを生成します。タプル (sql, params) を返します。 sql は SQL 文字列で、 params はクエリパ ... ラメータのリストまたはタプルです。 compiler は SQLCompiler オブジェクトで、他の式をコンパイルするための compile() メソッドを持っています。 connection はクエリの実行に ... 使用する接続です。 expression.as_sql() の呼び出しは通常正しくありません。 代わりに compiler.compile(expression) を使用する必要があります。 compiler.compile() メソッドは式のベンダ固有のメソッドの呼び出しを行います。 ... ... させる必要があります。 from django.db.models import Lookup class NotEqual ( Lookup ): lookup_name = "ne" def as_sql ( self , compiler , connection ): lhs , lhs_params = self . process_lhs ( compiler , connection ) rhs , rhs_params = self . process_rhs ( co ... ならない制約は、文字列 __ を決して含んではならないということです。 We then need to define the as_sql method. This takes a SQLCompiler object, called compiler , and the active database connection. SQLCompiler objects are not documented, but the only thing w ... では、左辺は Author モデルの name フィールドへの参照で、右辺は 'Jack' です。 process_lhs と process_rhs を呼び出して、前述の compiler オブジェクトを使用してSQLに必要な値に変換します。これらのメソッドは、SQL とその SQL に補間されるパラメーターを含むタプルを ... ... 。この属性が設定されていて、関数が異なる数の式で呼び出された場合、 TypeError が発生します。デフォルトは None です。 as_sql ( compiler , connection , function = None , template = None , arg_joiner = None , ** extra_context ) [ソース] データベース関数の SQL ... べきです。例えば: django/db/models/functions.py class ConcatPair ( Func ): ... function = "CONCAT" ... def as_mysql ( self , compiler , connection , ** extra_context ): return super () . as_sql ( compiler , connection , function = "CONCAT_WS" , template = ... ( query , allow_joins , reuse , summarize , for_save ) return c 次に、SQLを生成するメソッドを書きます: def as_sql ( self , compiler , connection , template = None ): sql_expressions , sql_params = [], [] for expression in self . expressions : sql , param ... ... igns_in_column_aliases feature flag is removed. The DatabaseOperations.binary_placeholder_sql() method now expects a query compiler as an extra positional argument and should return a two-elements tuple composed of an SQL format string and a tuple of ass ... e of associated parameters. This method should now expect to be provided expressions meant to be compiled via the provided compiler argument. The quote_name_unless_alias() method of SQLCompiler , the type of object passed as the compiler argument to the ... ... suffix , and data_type_check_constraints attributes have moved from the DatabaseCreation class to DatabaseWrapper . The SQLCompiler.as_sql() method now takes a subquery parameter ( #24164 ). The BaseDatabaseOperations.date_interval_sql() method now only ... erride either method. Support for storages that do not accept this argument will be removed in Django 1.10. qn replaced by compiler In previous Django versions, various internal ORM methods (mostly as_sql methods) accepted a qn (for "quote name") argumen ... erence to a function that quoted identifiers for sending to the database. In Django 1.8, that argument has been renamed to compiler and is now a full SQLCompiler instance. For backwards-compatibility, calling a SQLCompiler instance performs the same name ... ... 換えてください。 from django.db import models class Search ( models . Lookup ): lookup_name = "search" def as_mysql ( self , compiler , connection ): lhs , lhs_params = self . process_lhs ( compiler , connection ) rhs , rhs_params = self . process_rhs ( co ... .8 を見てください。ここには、プロジェクトからこれらの機能を削除する方法についても書かれています。 Support for calling a SQLCompiler directly as an alias for calling its quote_name_unless_alias method is removed. The cycle and firstof template tags are re ... ... ll cannot guarantee an HTML-safe result, as stated in the documentation). Fixed a regression in the django.contrib.gis SQL compiler for non-concrete fields ( #22250 ). Fixed ModelAdmin.preserve_filters when running a site with a URL prefix ( #21795 ). Fi ... ... derlying database operations remain synchronous, with contributions ongoing to push asynchronous support down into the SQL compiler, and integrate asynchronous database drivers. The new asynchronous queryset interface currently encapsulates the necessary ... ... bility problems that resulted, the second return element of the as_sql() method should now be a tuple: def as_sql ( self , compiler , connection ) -> tuple [ str , tuple ]: ... If your custom expressions support multiple versions of Django, you should ad ... 2026年10月08日