... ) age = models . IntegerField () class Publisher ( models . Model ): name = models . CharField ( max_length = 300 ) class Book ( models . Model ): name = models . CharField ( max_length = 300 ) pages = models . IntegerField () price = models . Decim ... . 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 ( pub ... ... ) age = models . IntegerField () class Publisher ( models . Model ): name = models . CharField ( max_length = 300 ) class Book ( models . Model ): name = models . CharField ( max_length = 300 ) pages = models . IntegerField () price = models . Decim ... . 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 ( pub ... ... . 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 t ... odes. We'll explain them below using these models: from django.db import models class Author ( models . Model ): ... class Book ( 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 ... ... . 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 t ... odes. We'll explain them below using these models: from django.db import models class Author ( models . Model ): ... class Book ( 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 ... ... usage of models, this change was reverted in Django 1.8.4 and replaced with a check during model.save() . For example: >>> book = Book . objects . create ( name = "Django" ) >>> book . author = Author ( name = "John" ) >>> book . save () Traceback (m ... ield now raises a ValueError . Previously, the assignment of an unsaved object would be silently ignored. For example: >>> book = Book . objects . create ( name = "Django" ) >>> book . author = Author ( name = "John" ) >>> book . author . save () >>> ... book . save () >>> Book . objects . get ( name = "Django" ) >>> book . author >>> Now, an error will be raised to prevent data ... ... usage of models, this change was reverted in Django 1.8.4 and replaced with a check during model.save() . For example: >>> book = Book . objects . create ( name = "Django" ) >>> book . author = Author ( name = "John" ) >>> book . save () Traceback (m ... ield now raises a ValueError . Previously, the assignment of an unsaved object would be silently ignored. For example: >>> book = Book . objects . create ( name = "Django" ) >>> book . author = Author ( name = "John" ) >>> book . author . save () >>> ... book . save () >>> Book . objects . get ( name = "Django" ) >>> book . author >>> Now, an error will be raised to prevent data ... ... lField () headshot = models . ImageField ( upload_to = "author_headshots" ) def __str__ ( self ): return self . name class Book ( models . Model ): title = models . CharField ( max_length = 100 ) authors = models . ManyToManyField ( "Author" ) publis ... 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 ()), ] 書く必要のある P ... ... lField () headshot = models . ImageField ( upload_to = "author_headshots" ) def __str__ ( self ): return self . name class Book ( models . Model ): title = models . CharField ( max_length = 100 ) authors = models . ManyToManyField ( "Author" ) publis ... 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 ()), ] 書く必要のある P ... ... ません。 そのために、URLconf 内で、本のリストビューへの URL を次のようにマッピングします。 from django.urls import path from books.views import BookListView urlpatterns = [ path ( "books/" , BookListView . as_view ()), ] そして、ビューには次のように書き ... ます。 from django.http import HttpResponse from django.views.generic import ListView from books.models import Book class BookListView ( ListView ): model = Book def head ( self , * args , ** kwargs ): last_book = self ... et () . latest ( "publication_date" ) response = HttpResponse ( # RFC 1123 date format. headers = { "Last-Modified" : last_book . publication_date . strftime ( " %a , %d %b %Y %H:%M:%S GMT" ) }, ) return response If the view is accessed from a GET re ... ... ints = [ models . UniqueConstraint ( fields = [ "first_name" , "last_name" ], name = "unique_first_last_name" , ), ] class Book ( models . Model ): name = models . CharField ( max_length = 100 ) author = models . ForeignKey ( Person , on_delete = mod ... els . CASCADE ) 通常は、 Book のシリアライズされたデータは、著者 (author) を参照するために整数を使うことになります。たとえば、JSON では、Book は次のよう ... にシリアライズされるでしょう。 ... { "pk" : 1 , "model" : "store.book" , "fields" : { "name" : "Mostly Harmless" , "author" : 42 }} ... これは、著者を参照するのに特に自然な方法とは言えません。 ... 2026年10月08日