... を CompositePrimaryKey に設定します: class Product ( models . Model ): name = models . CharField ( max_length = 100 ) class Order ( models . Model ): reference = models . CharField ( max_length = 20 , primary_key = True ) class OrderLineItem ( models . ... Model ): pk = models . CompositePrimaryKey ( "product_id" , "order_id" ) product = models . ForeignKey ( Product , on_delete = models . CASCADE ) order = models . ForeignKey ( Order , on_de ... . CASCADE ) quantity = models . IntegerField () これにより、Django はテーブル作成時に複合主キー( PRIMARY KEY (product_id, order_id) )を作成するようになります。 複合主キーは tuple (タプル)で表現されます: >>> product = Product . objects . create ( n ... ... デル Manager の latest() と earliest() メソッドで使用するデフォルトのフィールドを指定します。 実装例: # Latest by ascending order_date. get_latest_by = "order_date" # Latest by priority descending, order_date ascending. get_latest_by = [ "-priority" , ... "order_date" ] 詳しくは latest() のドキュメントを参照してください。 managed Options. managed デフォルトは True です。つまり、 Dja ... e を使って既存のモデルのコピーを作成することもできます。しかし、その場合はもっと良い方法があります。 プロキシモデル です。 order_with_respect_to Options. order_with_respect_to このオブジェクトを指定されたフィールドに対してソート可能にします。通常は Fo ... ... のセクションで後ほど詳しく説明します。 QuerySet クラスは、イントロスペクションのために以下のパブリックな属性を持っています: ordered [ソース] True if the QuerySet is ordered — i.e. has an order_by() clause or a default ordering on the model. False other ... wise. totally_ordered [ソース] New in Django 6.1. Returns True if the QuerySet is ordered and the ordering is deterministic. This requires tha ... t the ordering includes a field (or set of fields) that is unique and non-nullable. For queries involving a GROUP BY clause, the model ... ... ArticleFormSet = formset_factory ( ArticleForm , extra = 2 ) Formsets can be iterated and indexed, accessing forms in the order they were created. You can reorder the forms by overriding the default __iter__() and __getitem__() methods if needed. (Fo ... には、フォームセット内のフォームの順序およびフォームセットからのフォームの削除に役立つ 2 つのオプション引数があります。 can_order BaseFormSet. can_order デフォルト値: False 下記のように、ソート可能なフォームセットを作成できます: >>> from django.forms i ... mport formset_factory >>> from myapp.forms import ArticleForm >>> ArticleFormSet = formset_factory ( ArticleForm , can_order = True ) >>> formset = ArticleFormSet ( ... initial = [ ... { "title" : "Article #1" , "pub_date" : datetime . date ( 2008 ... ... " description = "Updates on changes and additions to police beat central." def items ( self ): return NewsItem . objects . order_by ( "-pub_date" )[: 5 ] def item_title ( self , item ): return item . title def item_description ( self , item ): return ... Feed ): title = "My articles" description_template = "feeds/articles.html" def items ( self ): return Article . objects . order_by ( "-pub_date" )[: 5 ] def get_context_data ( self , ** kwargs ): context = super () . get_context_data ( ** kwargs ) co ... ently reported in police beat %s " % obj . beat def items ( self , obj ): return Crime . objects . filter ( beat = obj ) . order_by ( "-crime_date" )[: 30 ] フィードの <title> , <link> , <description> を生成するために、 Django は title() , link() , de ... ... any . objects . annotate ( num_offerings = Count ( F ( "products" ) + F ( "services" ))) # Expressions can also be used in order_by(), either directly >>> Company . objects . order_by ( Length ( "name" ) . asc ()) >>> Company . objects . order_by ( Le ... Field >>> from django.db.models.functions import Length >>> CharField . register_lookup ( Length ) >>> Company . objects . order_by ( "name__length" ) # Boolean expression can be used directly in filters. >>> from django.db.models import Exists , Oute ... contacted がNULL) をコンタクトした企業の後に並べるには次のようにします: from django.db.models import F Company . objects . order_by ( F ( "last_contacted" ) . desc ( nulls_last = True )) 論理演算で F() を使う BooleanField を出力する F() 式は反転演算子 ... ... も持っています。 汎用的な集計関数 ArrayAgg class ArrayAgg ( expression , distinct = False , filter = None , default = None , order_by = () , ** extra ) null を含む値のリストを配列に連結して返すか、値がない場合は default を返す。 distinct オプションの真偽 ... 値引数で、配列の値を区別するかどうかを指定する。デフォルトは False です。 order_by 結果リスト内の要素の並び順を指定する、省略可能なフィールド名の文字列 (降順を示すオプションの ""-"" 接頭辞を含む) または ... 式 (もしくは文字列および/または式のタプルもしくはリスト)。 例: from django.db.models import F ArrayAgg ( "a_field" , order_by = "-some_field" ) ArrayAgg ( "a_field" , order_by = F ( "some_field" ) . desc ()) BitAnd class BitAnd ( expression , fi ... ... both situations when those templates are not found. It's still recommended as good practice to provide those templates in order to present pretty error pages to the user. django.contrib.auth provides a new signal that is emitted whenever a user fails ... s allow you to test equality for XML content at a semantic level, without caring for syntax differences (spaces, attribute order, etc.). RemoteUserMiddleware now forces logout when the REMOTE_USER header disappears during the same browser session. The ... archive class-based views YearArchiveView and MonthArchiveView were documented to provide a date_list sorted in ascending order in the context, like their function-based predecessors, but it actually was in descending order. In 1.5, the documented or ... ... ow_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" )[ ... ん 。 annotate() 句のアウトプットは QuerySet です。この QuerySet は、他の QuerySet の操作によって修正可能です。 filter() 、 order_by() などに加えて、別の annotate() を追加呼び出しすることもできます。 複数のアグリゲーションを統合する annotate() を用いて ... 方法を直感的に理解するのは難しいので、疑問がある場合は str(QuerySet.query) でSQLを検査し、たくさんのテストを書いてください。 order_by() アノテーションは、ソートの基準として使うことができます。 order_by() 句を定義すると、クエリの annotate() 句の中で定義さ ... ... 2026年10月08日