... 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 に対して集計を生 ... ... 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 ... ... 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 ... ... リを行うには、 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 ... ... 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 ... ... Potential SQL injection in QuerySet.annotate() , aggregate() , and extra() ¶ QuerySet.annotate() , aggregate() , and extra() methods were subject to SQL injection in c ... xpansion, as the **options argument. 目次 Django 2.2.28 リリースノート CVE-2022-28346: Potential SQL injection in QuerySet.annotate() , aggregate() , and extra() CVE-2022-28347: Potential SQL injection via QuerySet.explain(**options) on PostgreSQL 前のト ... ango 3.0 リリースノート 次のトピックへ Django 2.2.27 リリースノート 関連キーワード injection QuerySet dictionary Potential annotate aggregate extra methods subject using suitably crafted expansion explain options リリースノート high セキュリティ 問題 修正 ... ... Potential SQL injection in QuerySet.annotate() , aggregate() , and extra() ¶ QuerySet.annotate() , aggregate() , and extra() methods were subject to SQL injection in c ... xpansion, as the **options argument. 目次 Django 2.2.28 リリースノート CVE-2022-28346: Potential SQL injection in QuerySet.annotate() , aggregate() , and extra() CVE-2022-28347: Potential SQL injection via QuerySet.explain(**options) on PostgreSQL 前のト ... 0 リリースノート 次のトピックへ Django 2.2.27 リリースノート 関連キーワード injection QuerySet dictionary Django Potential annotate aggregate extra methods subject using suitably crafted expansion explain options リリースノート high セキュリティ 問題 関連 ... 2026年10月08日