... . CASCADE ) pubdate = models . DateField () class Store ( models . Model ): name = models . CharField ( max_length = 300 ) books = models . ManyToManyField ( Book ) チートシート ¶ お急ぎですか? 上のモデルを使った場合の一般的な集計クエリは以下のようにな ... ります: # Total number of books. >>> Book . objects . count () 2452 # Total number of books with publisher=BaloneyPress >>> Book . objects . filter ( publ ... isher__name = "BaloneyPress" ) . count () 73 # Average price across all books, provide default to be returned instead # of None if no books exist. >>> from django.db.models import Avg >>> Book . objec ... ... . CASCADE ) pubdate = models . DateField () class Store ( models . Model ): name = models . CharField ( max_length = 300 ) books = models . ManyToManyField ( Book ) チートシート ¶ お急ぎですか? 上のモデルを使った場合の一般的な集計クエリは以下のようにな ... ります: # Total number of books. >>> Book . objects . count () 2452 # Total number of books with publisher=BaloneyPress >>> Book . objects . filter ( publ ... isher__name = "BaloneyPress" ) . count () 73 # Average price across all books, provide default to be returned instead # of None if no books exist. >>> from django.db.models import Avg >>> Book . objec ... ... クトが開発できるようにして、複雑さを除くようにデザインされているからです。 ワークフロー ¶ The examples in this section use a books app with the following models: from django.db import models class Author ( models . Model ): name = models . CharField ( m ... 除するなど、モデルに変更を加えてから、 makemigrations を実行してください。 $ python manage.py makemigrations Migrations for 'books' : books/migrations/0003_auto.py: ~ Alter field author on book すると、あなたが書いたモデルがスキャンされ、現在のバージョン ... りに変更が行われるように、データベースに適用します: $ python manage.py migrate Operations to perform: Apply all migrations: books Running migrations: Applying books.0003_auto... OK マイグレーションを適用したら、マイグレーションとモデルの変更をバージョン ... ... クトが開発できるようにして、複雑さを除くようにデザインされているからです。 ワークフロー ¶ The examples in this section use a books app with the following models: from django.db import models class Author ( models . Model ): name = models . CharField ( m ... 除するなど、モデルに変更を加えてから、 makemigrations を実行してください。 $ python manage.py makemigrations Migrations for 'books' : books/migrations/0003_auto.py: ~ Alter field author on book すると、あなたが書いたモデルがスキャンされ、現在のバージョン ... りに変更が行われるように、データベースに適用します: $ python manage.py migrate Operations to perform: Apply all migrations: books Running migrations: Applying books.0003_auto... OK マイグレーションを適用したら、マイグレーションとモデルの変更をバージョン ... ... tion_date = models . DateField () 次に、ビューを定義しましょう。 # views.py from django.views.generic import ListView from books.models import Publisher class PublisherListView ( ListView ): model = Publisher 最後に、このビューを URL にフックさせます。 ... # urls.py from django.urls import path from books.views import PublisherListView urlpatterns = [ path ( "publishers/" , PublisherListView . as_view ()), ] 書く必要のある Py ... 書くこともできますが、この属性を省略した場合でも、Django はオブジェクト名から推論してくれます。この場合は、テンプレート名は "books/publisher_list.html" になります。"books" の部分はモデルを定義したアプリの名前から来ていますが、"publisher" の部分はモデル名 ... ... tion_date = models . DateField () 次に、ビューを定義しましょう。 # views.py from django.views.generic import ListView from books.models import Publisher class PublisherListView ( ListView ): model = Publisher 最後に、このビューを URL にフックさせます。 ... # urls.py from django.urls import path from books.views import PublisherListView urlpatterns = [ path ( "publishers/" , PublisherListView . as_view ()), ] 書く必要のある Py ... 書くこともできますが、この属性を省略した場合でも、Django はオブジェクト名から推論してくれます。この場合は、テンプレート名は "books/publisher_list.html" になります。"books" の部分はモデルを定義したアプリの名前から来ていますが、"publisher" の部分はモデル名 ... ... . Use QuerySet.fetch_mode() to set the fetch mode for model instances fetched by a QuerySet : from django.db import models books = Book . objects . fetch_mode ( models . FETCH_PEERS ) Fetch modes apply to: ForeignKey fields OneToOneField fields and th ... ( models . Model ): author = models . ForeignKey ( Author , on_delete = models . CASCADE ) ... …and this loop: for book in books : print ( book . author . name ) …where books is a QuerySet of Book instances using some fetch mode. FETCH_ONE ¶ Fetches t ... for the current instance only. This is the default mode. Using FETCH_ONE for the above example would use: 1 query to fetch books N queries, where N is the number of books, to fetch the missing author field …for a total of 1+N queries. This query patte ... ... . Use QuerySet.fetch_mode() to set the fetch mode for model instances fetched by a QuerySet : from django.db import models books = Book . objects . fetch_mode ( models . FETCH_PEERS ) Fetch modes apply to: ForeignKey fields OneToOneField fields and th ... ( models . Model ): author = models . ForeignKey ( Author , on_delete = models . CASCADE ) ... …and this loop: for book in books : print ( book . author . name ) …where books is a QuerySet of Book instances using some fetch mode. FETCH_ONE ¶ Fetches t ... for the current instance only. This is the default mode. Using FETCH_ONE for the above example would use: 1 query to fetch books N queries, where N is the number of books, to fetch the missing author field …for a total of 1+N queries. This query patte ... ... >> from django.db.models.functions import JSONObject >>> from django.contrib.postgres.expressions import ArraySubquery >>> books = Book . objects . filter ( author = OuterRef ( "pk" )) . values ( ... json = JSONObject ( title = "title" , pages = "page ... s" ) ... ) >>> author = Author . objects . annotate ( books = ArraySubquery ( books )) . first () >>> author . books [{'title': 'Solaris', 'pages': 204}, {'title': 'The Cyberiad', 'p ... のトピックへ PostgreSQL 固有のデータベース制約 次のトピックへ PostgreSQL 固有のモデルフィールド 関連キーワード ArraySubquery books title pages クエリ import author PostgreSQL contrib postgres expressions セット 構築 values models OuterRef JSONObject obj ... ... >> from django.db.models.functions import JSONObject >>> from django.contrib.postgres.expressions import ArraySubquery >>> books = Book . objects . filter ( author = OuterRef ( "pk" )) . values ( ... json = JSONObject ( title = "title" , pages = "page ... s" ) ... ) >>> author = Author . objects . annotate ( books = ArraySubquery ( books )) . first () >>> author . books [{'title': 'Solaris', 'pages': 204}, {'title': 'The Cyberiad', 'p ... クへ PostgreSQL 固有のデータベース制約 次のトピックへ PostgreSQL 固有のモデルフィールド 関連キーワード ArraySubquery django books title pages クエリ import author PostgreSQL contrib postgres expressions セット リスト 構築 values 必要 db models OuterRef ... 2026年10月08日