... されるまで順番に評価されます。一致した When() オブジェクトの result 式が返されます。 例: >>> >>> from datetime import date , timedelta >>> from django.db.models import Case , Value , When >>> Client . objects . create ( ... name = "Jane Doe" , ... account_t ... ype = Client . REGULAR , ... registered_on = date . today () - timedelta ( days = 36 ), ... ) >>> Client . objects . create ( ... name = "James Smith" , ... account_type = Client . GOLD , ... reg ... istered_on = date . today () - timedelta ( days = 5 ), ... ) >>> Client . objects . create ( ... name = "Jack Black" , ... account_type = Client . PLATINUM , ... r ... ... イムスタンプを追加します。これにより、署名された値が指定された期間内に作成されたことを確認できます: >>> from datetime import timedelta >>> from django.core.signing import TimestampSigner >>> signer = TimestampSigner () >>> value = signer . sign ( "hello" ) ... ge 15.5289158821 > 10 seconds >>> signer . unsign ( value , max_age = 20 ) 'hello' >>> signer . unsign ( value , max_age = timedelta ( seconds = 20 )) 'hello' class TimestampSigner ( * , key = None , sep = ':' , salt = None , algorithm = 'sha256' ) [ソース ... ss than max_age seconds ago, otherwise raises SignatureExpired . The max_age parameter can accept an integer or a datetime.timedelta object. sign_object ( obj , serializer = JSONSerializer , compress = False ) 複雑なデータ構造(例えば、リスト、タプル、また ... ... on instance with pub_date 30 days in the future >>> future_question = Question ( pub_date = timezone . now () + datetime . timedelta ( days = 30 )) >>> # was it published recently? >>> future_question . was_published_recently () True 将来のことは「最近(r ... ublished_recently() returns False for questions whose pub_date is in the future. """ time = timezone . now () + datetime . timedelta ( days = 30 ) future_question = Question ( pub_date = time ) self . assertIs ( future_question . was_published_recently () ... 返すようにしましょう。 polls/models.py def was_published_recently ( self ): now = timezone . now () return now - datetime . timedelta ( days = 1 ) <= self . pub_date <= now そして、もう一度テストを実行します。 Creating test database for alias 'default'... ... ... heritance ( #27407 ). Fixed a QuerySet.update() crash on SQLite when updating a DateTimeField with an F() expression and a timedelta ( #27544 ). Prevented LocaleMiddleware from redirecting on URLs that should return 404 when using prefix_default_language= ... ... put_field では、特定のモデルフィールドの文脈に依存せず、型変換を行うカスタムフィールドを使用することもできます。たとえば、 timedelta を使った日付計算を頻繁に行う場合、その変換処理を担当するカスタムフィールドを作成することで、データベース間で一貫した結果を ... なければならないことがあります。たとえば、直近一日以内に公開された投稿のすべてのコメントを返す場合: >>> from datetime import timedelta >>> from django.utils import timezone >>> one_day_ago = timezone . now () - timedelta ( days = 1 ) >>> posts = Post . obje ... かどうかのアノテーションを付けることができます: >>> from django.db.models import Exists , OuterRef >>> from datetime import timedelta >>> from django.utils import timezone >>> one_day_ago = timezone . now () - timedelta ( days = 1 ) >>> recent_comments = C ... ... ません。Django はモデルやーフォームでは aware な datetime オブジェクトを渡し、ほとんどの場合、新しい datetime オブジェクトは timedelta 計算を通じて他の日時から作成されます。唯一アプリケーションのコード内でよく作成される datetime は現在の時刻で、 timezone.no ... 場合は naive な datetime として、 USE_TZ = True の場合は aware な datetime として現在の日時を返します。必要に応じて datetime.timedelta を足したり引いたりすることができます。 多くの RuntimeWarning: DateTimeField received a naive datetime (YYYY-MM-DD HH:MM:SS ... paris . date () == new_york . date () (True, False) >>> paris - new_york , paris . date () - new_york . date () (datetime.timedelta(0), datetime.timedelta(1)) >>> paris datetime.datetime(2012, 3, 3, 1, 30, tzinfo=zoneinfo.ZoneInfo(key='Europe/Paris')) >> ... ... ... 10 ), start = now ) >>> Event . objects . create ( ... name = "Pub trip" , ages = ( 21 , None ), start = now - datetime . timedelta ( days = 1 ) ... ) そして NumericRange も: >>> from django.db.backends.postgresql.psycopg_any import NumericRange 包含関数 ... eTimeTZRange >>> Event . objects . filter ( ... start__contained_by = DateTimeTZRange ( ... timezone . now () - datetime . timedelta ( hours = 1 ), ... timezone . now () + datetime . timedelta ( hours = 1 ), ... ), ... ) <QuerySet [<Event: Soft play>]> ov ... ... nField class DurationField ( ** kwargs ) [ソース] デフォルトのウィジェット: TextInput 空の値: None 正規化後の値: Python の timedelta になります。 指定された値が timedelta に変換できる文字列であることを検証します。値は datetime.timedelta.min と datetime.ti ... ... ベース関数から構成する限り、より複雑な式を使用できます: month_due = models . DateField ( db_default = TruncMonth ( Now () + timedelta ( days = 90 ), output_field = models . DateField (), ) ) データベースのデフォルトは、他のフィールドやモデルを参照することは ... 間部分を削除します。これは、保存および比較の両方において当てはまります。 警告 PostgreSQL と MySQL では、 DateField に対して timedelta を用いた算術演算を行うと、 date ではなく datetime が返されます。これは、Python の timedelta が SQL の INTERVAL ``に変換され ... .db.models.functions import Cast qs = MyModel . objects . annotate ( previous_day = Cast ( F ( "date_field" ) - datetime . timedelta ( days = 1 ), output_field = DateField (), ) ) あるいは、 PostgreSQL だけですが、日数を整数演算として扱うこともできます。 ... 2026年10月08日