... を持つニュースアプリケーションを想像してみてください: from django.db import models STATUS_CHOICES = { "d" : "Draft" , "p" : "Published" , "w" : "Withdrawn" , } class Article ( models . Model ): title = models . CharField ( max_length = 100 ) body = models . ... CHOICES ) def __str__ ( self ): return self . title このようなモデルで行う一般的なタスクは記事のステータスを "draft "から "published "に更新することです。管理画面で一度に1つの記事を更新するのは簡単ですが、記事のグループを一括で公開するのは面倒です。そこで ... 、記事のステータスを "published " に変更するアクションを書いてみましょう。 アクション関数を書く ¶ 最初に、管理画面からアクションがトリガーされたときに呼び ... ... を持つニュースアプリケーションを想像してみてください: from django.db import models STATUS_CHOICES = { "d" : "Draft" , "p" : "Published" , "w" : "Withdrawn" , } class Article ( models . Model ): title = models . CharField ( max_length = 100 ) body = models . ... CHOICES ) def __str__ ( self ): return self . title このようなモデルで行う一般的なタスクは記事のステータスを "draft "から "published "に更新することです。管理画面で一度に1つの記事を更新するのは簡単ですが、記事のグループを一括で公開するのは面倒です。そこで ... 、記事のステータスを "published " に変更するアクションを書いてみましょう。 アクション関数を書く ¶ 最初に、管理画面からアクションがトリガーされたときに呼び ... ... テスト作成 ¶ バグを見つけたとき ¶ 運よく、 polls のアプリケーションにはすぐに修正可能な小さなバグがありました。 Question.was_published_recently() のメソッドは Question が昨日以降に作成された場合に True を返すのですが(適切な動作)、 Question の pub_date が未来 ... he future >>> future_question = Question ( pub_date = timezone . now () + datetime . timedelta ( days = 30 )) >>> # was it published recently? >>> future_question . was_published_recently () True 将来のことは「最近(recent)」ではないため、これは明らかに間 ... estCase from django.utils import timezone from .models import Question class QuestionModelTests ( TestCase ): def test_was_published_recently_with_future_question ( self ): """ was_published_recently() returns False for questions whose pub_date is in the ... ... テスト作成 ¶ バグを見つけたとき ¶ 運よく、 polls のアプリケーションにはすぐに修正可能な小さなバグがありました。 Question.was_published_recently() のメソッドは Question が昨日以降に作成された場合に True を返すのですが(適切な動作)、 Question の pub_date が未来 ... he future >>> future_question = Question ( pub_date = timezone . now () + datetime . timedelta ( days = 30 )) >>> # was it published recently? >>> future_question . was_published_recently () True 将来のことは「最近(recent)」ではないため、これは明らかに間 ... estCase from django.utils import timezone from .models import Question class QuestionModelTests ( TestCase ): def test_was_published_recently_with_future_question ( self ): """ was_published_recently() returns False for questions whose pub_date is in the ... ... Admin ( admin . ModelAdmin ): # ... list_display = [ "question_text" , "pub_date" ] 念のため、 チュートリアル その2 の was_published_recently() メソッドも含めましょう。 polls/admin.py ¶ class QuestionAdmin ( admin . ModelAdmin ): # ... list_display = [ "q ... uestion_text" , "pub_date" , "was_published_recently" ] これで、Question のチェンジリストのページは以下のようになります: カラムのヘッダをクリックすると、カラムの値に応 ... じてエントリを並べ換えできます。ただし was_published_recently ヘッダは例外であり、これはメソッドの戻り値を使った並べ換えをサポートしていないからです。 was_published_recently の ... ... Admin ( admin . ModelAdmin ): # ... list_display = [ "question_text" , "pub_date" ] 念のため、 チュートリアル その2 の was_published_recently() メソッドも含めましょう。 polls/admin.py ¶ class QuestionAdmin ( admin . ModelAdmin ): # ... list_display = [ "q ... uestion_text" , "pub_date" , "was_published_recently" ] これで、Question のチェンジリストのページは以下のようになります: カラムのヘッダをクリックすると、カラムの値に応 ... じてエントリを並べ換えできます。ただし was_published_recently ヘッダは例外であり、これはメソッドの戻り値を使った並べ換えをサポートしていないからです。 was_published_recently の ... ... 合は default を、それ以外の場合は False を返します。 使用例: class Comment ( models . Model ): body = models . TextField () published = models . BooleanField () rank = models . IntegerField () >>> from django.db.models import Q >>> from django.contrib.post ... gres.aggregates import BoolAnd >>> Comment . objects . aggregate ( booland = BoolAnd ( "published" )) {'booland': False} >>> Comment . objects . aggregate ( booland = BoolAnd ( Q ( rank__lt = 100 ))) {'booland': True} Bo ... 合は default を、そうでない場合は False を返します。 使用例: class Comment ( models . Model ): body = models . TextField () published = models . BooleanField () rank = models . IntegerField () >>> from django.db.models import Q >>> from django.contrib.post ... ... 合は default を、それ以外の場合は False を返します。 使用例: class Comment ( models . Model ): body = models . TextField () published = models . BooleanField () rank = models . IntegerField () >>> from django.db.models import Q >>> from django.contrib.post ... gres.aggregates import BoolAnd >>> Comment . objects . aggregate ( booland = BoolAnd ( "published" )) {'booland': False} >>> Comment . objects . aggregate ( booland = BoolAnd ( Q ( rank__lt = 100 ))) {'booland': True} Bo ... 合は default を、そうでない場合は False を返します。 使用例: class Comment ( models . Model ): body = models . TextField () published = models . BooleanField () rank = models . IntegerField () >>> from django.db.models import Q >>> from django.contrib.post ... ... tion ( models . Model ): question_text = models . CharField ( max_length = 200 ) pub_date = models . DateTimeField ( "date published" ) class Choice ( models . Model ): question = models . ForeignKey ( Question , on_delete = models . CASCADE ) choice_text ... t datetime from django.db import models from django.utils import timezone class Question ( models . Model ): # ... def was_published_recently ( self ): return self . pub_date >= timezone . now () - datetime . timedelta ( days = 1 ) import datetime と from ... . objects . filter ( question_text__startswith = "What" ) <QuerySet [<Question: What's up?>]> # Get the question that was published this year. >>> from django.utils import timezone >>> current_year = timezone . now () . year >>> Question . objects . get ... ... ( models . Model ): ... class Entry ( models . Model ): blog = models . ForeignKey ( Blog , on_delete = models . CASCADE ) published = models . DateTimeField ( default = datetime . datetime . now ) ... 最新のブログエントリーを表示するフロントページが、新し ... を追加したときだけ変更されるのであれば、最終更新時刻をとても速く計算できます。そのブログに関連するすべてのエントリーの最新の published 日付が必要です。これを行う一つの方法は次のようなものです。 def latest_entry ( request , blog_id ): return Entry . objects ... . filter ( blog = blog_id ) . latest ( "published" ) . published この機能を使えば、トップページビューで変更されていないページを早期に検出できます。 from django.views.decora ... 2026年10月08日