... with a count of books as a "num_books" attribute. >>> from django.db.models import Count >>> pubs = Publisher . objects . annotate ( num_books = Count ( "book" )) >>> pubs <QuerySet [<Publisher: BaloneyPress>, <Publisher: SalamiPress>, ...]> >>> pubs [ ... k__rating__gt = 5 )) >>> below_5 = Count ( "book" , filter = Q ( book__rating__lte = 5 )) >>> pubs = Publisher . objects . annotate ( below_5 = below_5 ) . annotate ( above_5 = above_5 ) >>> pubs [ 0 ] . above_5 23 >>> pubs [ 0 ] . below_5 12 # The top 5 ... publishers, in order by number of books. >>> pubs = Publisher . objects . annotate ( num_books = Count ( "book" )) . order_by ( "-num_books" )[: 5 ] >>> pubs [ 0 ] . num_books 1323 QuerySet に対して集計を生 ... ... functions import Cast >>> Author . objects . create ( age = 25 , name = "Margaret Smith" ) >>> author = Author . objects . annotate ( ... age_as_float = Cast ( "age" , output_field = FloatField ()), ... ) . get () >>> print ( author . age_as_float ) 25.0 ... rt Coalesce >>> Author . objects . create ( name = "Margaret Smith" , goes_by = "Maggie" ) >>> author = Author . objects . annotate ( screen_name = Coalesce ( "alias" , "goes_by" , "name" )) . get () >>> print ( author . screen_name ) Maggie >>> # Preven ... >> comment = Comment . objects . create ( body = "No, Least is better." , blog = blog ) >>> comments = Comment . objects . annotate ( last_updated = Greatest ( "modified" , "blog__modified" )) >>> annotated_comment = comments . get () annotated_comment.l ... ... リを行うには、 SearchVector を使用します: >>> from django.contrib.postgres.search import SearchVector >>> Entry . objects . annotate ( ... search = SearchVector ( "body_text" , "blog__tagline" ), ... ) . filter ( search = "Cheese" ) <QuerySet [<Entry: Che ... 検索ドキュメントにすべて含まれます。 SearchVector オブジェクトは組み合わせて再利用できます。たとえば: >>> Entry . objects . annotate ( ... search = SearchVector ( "body_text" ) + SearchVector ( "blog__tagline" ), ... ) . filter ( search = "Cheese" ) <Quer ... chRank , SearchVector >>> vector = SearchVector ( "body_text" ) >>> query = SearchQuery ( "cheese" ) >>> Entry . objects . annotate ( rank = SearchRank ( vector , query )) . order_by ( "-rank" ) <QuerySet [<Entry: Cheese on Toast recipes>, <Entry: Pizza ... ... pany to seat all employees? >>> company = ( ... Company . objects . filter ( num_employees__gt = F ( "num_chairs" )) ... . annotate ( chairs_needed = F ( "num_employees" ) - F ( "num_chairs" )) ... . first () ... ) >>> company . num_employees 120 >>> com ... company = Company . objects . create ( name = "Google" , ticker = Upper ( Value ( "goog" ))) >>> company . ticker 'GOOG' # Annotate models with an aggregated value. Both forms # below are equivalent. >>> Company . objects . annotate ( num_products = Coun ... t ( "products" )) >>> Company . objects . annotate ( num_products = Count ( F ( "products" ))) # Aggregates can contain complex computations also >>> Company . objects . ann ... ... ... and extra() QuerySet.annotate() , aggregate() , and extra() methods were subject to SQL injection in column aliases, using a suitably crafted dictionary ... ... ... ort Distance >>> pnt = AustraliaCity . objects . get ( name = "Hobart" ) . point >>> for city in AustraliaCity . objects . annotate ( distance = Distance ( "point" , pnt )): ... print ( city . name , city . distance ) ... Wollongong 990071.220408 m Shell ... 構造体のキーコンテンツ geometry だけであることに注意してください。 GeoJSON シリアライザ も参照。 例: >>> City . objects . annotate ( json = AsGeoJSON ( "point" )) . get ( name = "Chicago" ) . json {"type":"Point","coordinates":[-87.65018,41.85039]} キー ... ルドまたは式を受け取り、そのジオメトリの Geographic Markup Language (GML) 表現を返します。 例: >>> qs = Zipcode . objects . annotate ( gml = AsGML ( "poly" )) >>> print ( qs [ 0 ] . gml ) <gml:Polygon srsName="EPSG:4326"><gml:OuterBoundaryIs>-147.78711,70 ... ... ogic When passing a QuerySet to the in lookup, ensure the subquery does not return NULL values. See this note for details. annotate() annotate ( * args , ** kwargs ) Annotates each object in the QuerySet with the provided list of query expressions or Q() ... オブジェクト . Each object can be annotated with: Value() で指定する単純な値。 F() で指定するモデル(または関連モデル)のフィールドへの参照。 Q() で指定する真偽値(判 ... 定式)。 QuerySet 内のオブジェクトに関連付けられたオブジェクトに対して計算された集約式(平均、合計など)の結果。 annotate() に渡される引数はそれぞれがアノテーションとなって、返される QuerySet 内の各オブジェクトに追加されます。 Djangoが提供する集 ... ... e() , および extra() 利用時の SQL インジェクションの可能性 QuerySet.annotate() , alias() , aggregate() , および extra() メソッドには、列エイリアスにおける SQL インジェクションの脆弱性が存在していまし ... 2026年10月08日