... max_length = 300 ) pages = models . IntegerField () price = models . DecimalField ( max_digits = 10 , decimal_places = 2 ) rating = models . FloatField () authors = models . ManyToManyField ( Author ) publisher = models . ForeignKey ( Publisher , on_de ... ss>, <Publisher: SalamiPress>, ...]> >>> pubs [ 0 ] . num_books 73 # Each publisher, with a separate count of books with a rating above and below 5 >>> from django.db.models import Q >>> above_5 = Count ( "book" , filter = Q ( book__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 ... ... max_length = 300 ) pages = models . IntegerField () price = models . DecimalField ( max_digits = 10 , decimal_places = 2 ) rating = models . FloatField () authors = models . ManyToManyField ( Author ) publisher = models . ForeignKey ( Publisher , on_de ... ss>, <Publisher: SalamiPress>, ...]> >>> pubs [ 0 ] . num_books 73 # Each publisher, with a separate count of books with a rating above and below 5 >>> from django.db.models import Q >>> above_5 = Count ( "book" , filter = Q ( book__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 ... ... ングでアノテーションを付ける場合: >>> from django.db.models import Avg , F , Window >>> Movie . objects . annotate ( ... avg_rating = Window ( ... expression = Avg ( "rating" ), ... partition_by = [ F ( "studio" ), F ( "genre" )], ... order_by = "release ... by" : [ F ( "studio" ), F ( "genre" )], ... "order_by" : "released__year" , ... } >>> Movie . objects . annotate ( ... avg_rating = Window ( ... expression = Avg ( "rating" ), ... ** window , ... ), ... best = Window ( ... expression = Max ( "rating" ) ... , ... ** window , ... ), ... worst = Window ( ... expression = Min ( "rating" ), ... ** window , ... ), ... ) ウィンドウ関数に対するフィルタリングは、ルックアップが分離型でない(コネクタとして OR や ... ... ングでアノテーションを付ける場合: >>> from django.db.models import Avg , F , Window >>> Movie . objects . annotate ( ... avg_rating = Window ( ... expression = Avg ( "rating" ), ... partition_by = [ F ( "studio" ), F ( "genre" )], ... order_by = "release ... by" : [ F ( "studio" ), F ( "genre" )], ... "order_by" : "released__year" , ... } >>> Movie . objects . annotate ( ... avg_rating = Window ( ... expression = Avg ( "rating" ), ... ** window , ... ), ... best = Window ( ... expression = Max ( "rating" ) ... , ... ** window , ... ), ... worst = Window ( ... expression = Min ( "rating" ), ... ** window , ... ), ... ) ウィンドウ関数に対するフィルタリングは、ルックアップが分離型でない(コネクタとして OR や ... ... r ) number_of_comments = models . IntegerField ( default = 0 ) number_of_pingbacks = models . IntegerField ( default = 0 ) rating = models . IntegerField ( default = 5 ) def __str__ ( self ): return self . headline オブジェクトを作成する ¶ Djangoでは、 ... ク数とコメント数の合計より少ないエントリーをすべて見つけるには、次のようなクエリを発行します: >>> Entry . objects . filter ( rating__lt = F ( "number_of_comments" ) + F ( "number_of_pingbacks" )) F() オブジェクトのリレーションシップに 2重アンダースコア表 ... m django.db.models import OuterRef , Subquery , Sum >>> Entry . objects . values ( "pub_date__year" ) . annotate ( ... top_rating = Subquery ( ... Entry . objects . filter ( ... pub_date__year = OuterRef ( "pub_date__year" ), ... ) ... . order_by ( "-r ... ... r ) number_of_comments = models . IntegerField ( default = 0 ) number_of_pingbacks = models . IntegerField ( default = 0 ) rating = models . IntegerField ( default = 5 ) def __str__ ( self ): return self . headline オブジェクトを作成する ¶ Djangoでは、 ... ク数とコメント数の合計より少ないエントリーをすべて見つけるには、次のようなクエリを発行します: >>> Entry . objects . filter ( rating__lt = F ( "number_of_comments" ) + F ( "number_of_pingbacks" )) F() オブジェクトのリレーションシップに 2重アンダースコア表 ... m django.db.models import OuterRef , Subquery , Sum >>> Entry . objects . values ( "pub_date__year" ) . annotate ( ... top_rating = Subquery ( ... Entry . objects . filter ( ... pub_date__year = OuterRef ( "pub_date__year" ), ... ) ... . order_by ( "-r ... ... estaurant ( pk = 1 , name = "Bob's Cafe" ) . save ( force_insert = ( Place ,)) Restaurant ( pk = 1 , name = "Bob's Cafe" , rating = 4 ) . save ( force_insert = ( Place , Rating )) force_insert=(models.Model,) を渡すことで、すべての親モデルに対して INSE ... ... estaurant ( pk = 1 , name = "Bob's Cafe" ) . save ( force_insert = ( Place ,)) Restaurant ( pk = 1 , name = "Bob's Cafe" , rating = 4 ) . save ( force_insert = ( Place , Rating )) force_insert=(models.Model,) を渡すことで、すべての親モデルに対して INSE ... ... を遅延読み込みリストに追加します: # Defers both the body and headline fields. Entry . objects . defer ( "body" ) . filter ( rating = 5 ) . defer ( "headline" ) フィールドが遅延読み込みリストに追加される順番は重要ではありません。すでに遅延読み込みに指定さ ... ィールドだけが考慮されることになります: # This will defer all fields except the headline. Entry . objects . only ( "body" , "rating" ) . only ( "headline" ) defer() は段階的に(遅延読み込みリストにフィールドを追加しながら)動作するので、 only() と defer( ... ... migrations" , "0001_initial" )] operations = [ migrations . DeleteModel ( "Tribble" ), migrations . AddField ( "Author" , "rating" , models . IntegerField ( default = 0 )), ] Django が (Python モジュールとして) マイグレーションファイルを読み込んだ時に最 ... 2026年10月08日