... に掲載することができ、一つの Publication は複数の Article オブジェクトを持ちます: from django.db import models class Publication ( models . Model ): title = models . CharField ( ma ... x_length = 30 ) class Meta : ordering = [ "title" ] def __str__ ( self ): return self . title class Article ( models . Model ): headline = models . CharField ( max_length = 100 ) publications = models . ManyToManyField ( Publicati ... Publication ( title = "Science News" ) >>> p2 . save () >>> p3 = Publication ( title = "Science Weekly" ) >>> p3 . save () Article を一つ作ります: >>> a1 = Article ( headline = "Django lets you build web apps easily" ) 保存するまでは Publication と関連付 ... ... に掲載することができ、一つの Publication は複数の Article オブジェクトを持ちます: from django.db import models class Publication ( models . Model ): title = models . CharField ( ma ... x_length = 30 ) class Meta : ordering = [ "title" ] def __str__ ( self ): return self . title class Article ( models . Model ): headline = models . CharField ( max_length = 100 ) publications = models . ManyToManyField ( Publicati ... Publication ( title = "Science News" ) >>> p2 . save () >>> p3 = Publication ( title = "Science Weekly" ) >>> p3 . save () Article を一つ作ります: >>> a1 = Article ( headline = "Django lets you build web apps easily" ) 保存するまでは Publication と関連付 ... ... 連付けることができますが、 Article は一つの Reporter オブジェクトしか持つことができません: from django.db import models class Reporter ( models . Model ): fi ... = 30 ) email = models . EmailField () def __str__ ( self ): return f " { self . first_name } { self . last_name } " class Article ( models . Model ): headline = models . CharField ( max_length = 100 ) pub_date = models . DateField () reporter = models ... r . save () >>> r2 = Reporter ( first_name = "Paul" , last_name = "Jones" , email = "paul@example.com" ) >>> r2 . save () Article を作成する: >>> from datetime import date >>> a = Article ( id = None , headline = "This is a test" , pub_date = date ( 20 ... ... 連付けることができますが、 Article は一つの Reporter オブジェクトしか持つことができません: from django.db import models class Reporter ( models . Model ): fi ... = 30 ) email = models . EmailField () def __str__ ( self ): return f " { self . first_name } { self . last_name } " class Article ( models . Model ): headline = models . CharField ( max_length = 100 ) pub_date = models . DateField () reporter = models ... r . save () >>> r2 = Reporter ( first_name = "Paul" , last_name = "Jones" , email = "paul@example.com" ) >>> r2 . save () Article を作成する: >>> from datetime import date >>> a = Article ( id = None , headline = "This is a test" , pub_date = date ( 20 ... ... ンページを描画するためのビューです。 注釈 このページの例のいくつかは、 myapp/models.py 内で下記の通り Article モデルが定義されていると仮定します: from django.db import models from django.urls import reverse class Article ( models . ... models . CharField ( max_length = 200 ) pub_date = models . DateField () def get_absolute_url ( self ): return reverse ( "article-detail" , kwargs = { "pk" : self . pk }) ArchiveIndexView ¶ class ArchiveIndexView [ソース] ¶ "最新の"オブジェクトを、日付 ... pp/urls.py : from django.urls import path from django.views.generic.dates import ArchiveIndexView from myapp.models import Article urlpatterns = [ path ( "archive/" , ArchiveIndexView . as_view ( model = Article , date_field = "pub_date" ), name = "arti ... ... ンページを描画するためのビューです。 注釈 このページの例のいくつかは、 myapp/models.py 内で下記の通り Article モデルが定義されていると仮定します: from django.db import models from django.urls import reverse class Article ( models . ... models . CharField ( max_length = 200 ) pub_date = models . DateField () def get_absolute_url ( self ): return reverse ( "article-detail" , kwargs = { "pk" : self . pk }) ArchiveIndexView ¶ class ArchiveIndexView [ソース] ¶ "最新の"オブジェクトを、日付 ... pp/urls.py : from django.urls import path from django.views.generic.dates import ArchiveIndexView from myapp.models import Article urlpatterns = [ path ( "archive/" , ArchiveIndexView . as_view ( model = Article , date_field = "pub_date" ), name = "arti ... ... ( models . Model ): full_name = models . CharField ( max_length = 70 ) def __str__ ( self ): return self . full_name class Article ( models . Model ): pub_date = models . DateField () headline = models . CharField ( max_length = 200 ) content = models . ... で生成され、コードを作成する必要はありません: # Import the models we created from our "news" app >>> from news.models import Article , Reporter # No reporters are in the system yet. >>> Reporter . objects . all () <QuerySet []> # Create a new Reporter. >> ... . get ( id = 2 ) Traceback (most recent call last): ... DoesNotExist : Reporter matching query does not exist. # Create an article. >>> from datetime import date >>> a = Article ( ... pub_date = date . today (), headline = "Django is cool" , content = " ... ... ( models . Model ): full_name = models . CharField ( max_length = 70 ) def __str__ ( self ): return self . full_name class Article ( models . Model ): pub_date = models . DateField () headline = models . CharField ( max_length = 200 ) content = models . ... で生成され、コードを作成する必要はありません: # Import the models we created from our "news" app >>> from news.models import Article , Reporter # No reporters are in the system yet. >>> Reporter . objects . all () <QuerySet []> # Create a new Reporter. >> ... . get ( id = 2 ) Traceback (most recent call last): ... DoesNotExist : Reporter matching query does not exist. # Create an article. >>> from datetime import date >>> a = Article ( ... pub_date = date . today (), headline = "Django is cool" , content = " ... ... o_response() 例 myapp/views.py : from django.utils import timezone from django.views.generic.detail import DetailView from articles.models import Article class ArticleDetailView ( DetailView ): model = Article def get_context_data ( self , ** kwargs ): ... ta ( ** kwargs ) context [ "now" ] = timezone . now () return context 例 myapp/urls.py : from django.urls import path from article.views import ArticleDetailView urlpatterns = [ path ( "<slug:slug>/" , ArticleDetailView . as_view (), name = "article-det ... ail" ), ] 例 myapp/article_detail.html : < h1 > {{ object.headline }} </ h1 > < p > {{ object.content }} </ p > < p > Reporter: {{ object.reporter }} ... ... o_response() 例 myapp/views.py : from django.utils import timezone from django.views.generic.detail import DetailView from articles.models import Article class ArticleDetailView ( DetailView ): model = Article def get_context_data ( self , ** kwargs ): ... ta ( ** kwargs ) context [ "now" ] = timezone . now () return context 例 myapp/urls.py : from django.urls import path from article.views import ArticleDetailView urlpatterns = [ path ( "<slug:slug>/" , ArticleDetailView . as_view (), name = "article-det ... ail" ), ] 例 myapp/article_detail.html : < h1 > {{ object.headline }} </ h1 > < p > {{ object.content }} </ p > < p > Reporter: {{ object.reporter }} ... 2026年10月08日