... [<Article: John's second story>, <Article: This is a test>]> >>> Article . objects . filter ( reporter__in = [ 1 , 2 ]) . distinct () <QuerySet [<Article: John's second story>, <Article: Paul's story>, <Article: This is a test>]> >>> Article . objects . ... filter ( reporter__in = [ r , r2 ]) . distinct () <QuerySet [<Article: John's second story>, <Article: Paul's story>, <Article: This is a test>]> インスタンスのリテラルの ... もできます: >>> Article . objects . filter ( ... reporter__in = Reporter . objects . filter ( first_name = "John" ) ... ) . distinct () <QuerySet [<Article: John's second story>, <Article: This is a test>]> 逆方向のクエリ: >>> Reporter . objects . filter ... ... る引数 ( nulls_first と nulls_last )をとります。 Be cautious when ordering by fields in related models if you are also using distinct() . See the note in distinct() for an explanation of how related model ordering can change the expected results. 注釈 複数 ... () on it has no real effect (the ordering was undefined prior to calling reverse() , and will remain undefined afterward). distinct() distinct ( * fields ) SQL クエリで SELECT DISTINCT を使用した新しい QuerySet を返します。これにより、クエリ結果から重複し ... クエリが複数のテーブルにまたがっている場合、 QuerySet が評価されたときに重複した結果を得る可能性があります。このような場合は distinct() を使用します。 注釈 order_by() の呼び出しで使用されるフィールドはすべて、SQL の SELECT 列に含まれます。これは distinct() ... ... es Python>, <Article: NASA uses Python>]> >>> Article . objects . filter ( publications__title__startswith = "Science" ) . distinct () <QuerySet [<Article: NASA uses Python>]> count() 関数は distinct() も尊重します: >>> Article . objects . filter ( publi ... le__startswith = "Science" ) . count () 2 >>> Article . objects . filter ( publications__title__startswith = "Science" ) . distinct () . count () 1 >>> Article . objects . filter ( publications__in = [ 1 , 2 ]) . distinct () <QuerySet [<Article: Django l ... u build web apps easily>, <Article: NASA uses Python>]> >>> Article . objects . filter ( publications__in = [ p1 , p2 ]) . distinct () <QuerySet [<Article: Django lets you build web apps easily>, <Article: NASA uses Python>]> 逆の多対多クエリもサポートさ ... ... を持ち、ほとんどの集計関数は default キーワード引数も持っています。 汎用的な集計関数 ArrayAgg class ArrayAgg ( expression , distinct = False , filter = None , default = None , order_by = () , ** extra ) null を含む値のリストを配列に連結して返すか、値がない ... 場合は default を返す。 distinct オプションの真偽値引数で、配列の値を区別するかどうかを指定する。デフォルトは False です。 order_by 結果リスト内の要素の並び ... t . objects . aggregate ( boolor = BoolOr ( Q ( rank__gt = 2 ))) {'boolor': False} JSONBAgg class JSONBAgg ( expressions , distinct = False , filter = None , default = None , order_by = () , ** extra ) 入力値を JSON 配列として返します。値がない場合は def ... ... aint ( * expressions , fields = () , name , condition = None , deferrable = None , include = None , opclasses = () , nulls_distinct = None , violation_error_code = None , violation_error_message = None ) [ソース] Creates a uniqueness guarantee in the dat ... ar_pattern_ops を使用する一意なインデックスを作成します。 opclasses はPostgreSQL以外のデータベースでは無視されます。 nulls_distinct UniqueConstraint. nulls_distinct ユニーク制約の対象となる NULL 値を含む行を、互いに異なる行とみなすかどうかを指定します。デ ... e となるデータベースのデフォルト値を使用します。 例: UniqueConstraint ( name = "ordering" , fields = [ "ordering" ], nulls_distinct = False ) これは、 ordering カラムに NULL 値を格納できるのは1行だけというユニーク制約を作成します。 Unique constraints wit ... ... objects filtered against multi-valued relationships ( #32682 ). The admin changelist now uses Exists() instead of QuerySet.distinct() because calling delete() after distinct() is not allowed in Django 3.2 to address a data loss possibility. Fixed a regre ... ... ] . authors__count 6 >>> q [ 0 ] . store__count 6 ほとんどの集計方法では、この問題を逃れるすべはありませんが、 Count では distinct が助けになります: >>> q = Book . objects . annotate ( ... Count ( "authors" , distinct = True ), Count ( "store" , distinc ... 、評価は1である。 以下は Count 集計を使った例です: >>> a , b = Publisher . objects . annotate ( num_books = Count ( "book" , distinct = True )) . filter ( ... book__rating__gt = 3.0 ... ) >>> a , a . num_books (<Publisher: A>, 2) >>> b , b . num_books (<Pu ... 除外されます。 最初のクエリでは、アノテーションをフィルタよりも先に付けるため、フィルタはアノテーションに影響を与えません。 distinct=True は クエリのバグ を避けるために必要です。 2つ目のクエリは、出版社ごとにレーティングが3.0を超える書籍の数をカウントしま ... ... y reducing the number of database round trips. Added SQLite support for the StdDev and Variance functions. The handling of DISTINCT aggregation is added to the Aggregate class. Adding allow_distinct = True as a class attribute on Aggregate subclasses all ... ows a distinct keyword argument to be specified on initialization to ensure that the aggregate function is only called for each distinct ... ... ) + Count ( "num_managers" ) ) Aggregate の API は以下の通りです: class Aggregate ( * expressions , output_field = None , distinct = False , filter = None , default = None , order_by = None , ** extra ) [ソース] template この aggregate 用に生成される SQ ... L を記述する、フォーマット文字列としてのクラス属性です。デフォルトは '%(function)s(%(distinct)s%(expressions)s)' です。 function 生成される集計関数を記述するクラス属性です。具体的には、 function が template 内の func ... は None です。 window_compatible ほとんどの集計関数は Window のソース式として使用できるので、デフォルトは True です。 allow_distinct この集計関数が distinct キーワード引数を渡すことができるかどうかを決定するクラス属性。 False (デフォルト) に設定されている ... ... アップが指定されていない場合、Django は change__abs=27 を change__abs__exact=27 として解釈します。 これにより ORDER BY 句と DISTINCT ON 句の使用も可能です。例えば Experiment.objects.order_by('change__abs') は以下の SQL を生成します。 SELECT ... ORDER BY A ... BS ( "experiments" . "change" ) ASC そして、PosgreSQL のようにフィールドを DISTINCT することもサポートしています。 Experiment.objects.distinct('change__abs') は以下の SQL を生成します。 SELECT ... DISTINCT ... 2026年10月08日