summaryrefslogtreecommitdiff
path: root/docs/ref/models/fields.txt
diff options
context:
space:
mode:
authorCha Hwa Young <chahwayoung214@gmail.com>2025-10-13 02:29:15 +0900
committerJacob Walls <jacobtylerwalls@gmail.com>2025-11-26 08:34:35 -0500
commit122b2dc569bb2074367188621e1acd87f35d91a1 (patch)
treeb92730a7dcc227010fd8dd8c16bfa2d1a5508e76 /docs/ref/models/fields.txt
parent0868b7151a3921c6a1d8d946ab80cee5702d8f3b (diff)
[6.0.x] Fixed #31506 -- Clarified that ExpressionWrapper does not perform database casts.
Added warning in DateField documentation about type differences when using timedelta on PostgreSQL and MySQL. Mentioned Cast() and integer arithmetic solutions. Backport of 55af4749b9a48b2978e893e7d7be313c0b2abdb1 from main.
Diffstat (limited to 'docs/ref/models/fields.txt')
-rw-r--r--docs/ref/models/fields.txt33
1 files changed, 33 insertions, 0 deletions
diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt
index 95c344476f..c8fc074e23 100644
--- a/docs/ref/models/fields.txt
+++ b/docs/ref/models/fields.txt
@@ -841,6 +841,39 @@ exclusive. Any combination of these options will result in an error.
instance, removing its time component. This is true for both storage and
comparison.
+.. warning::
+
+ On PostgreSQL and MySQL, arithmetic operations on a ``DateField`` with a
+ :class:`~datetime.timedelta` return a ``datetime`` instead of a ``date``.
+ This occurs because Python's ``timedelta`` is converted to SQL
+ ``INTERVAL``, and the SQL operation ``date +/- interval`` returns a
+ ``timestamp`` on these databases.
+
+ To ensure a ``date`` result, use one of the following approaches. Either
+ explicitly cast the result to a date::
+
+ import datetime
+ from django.db.models import DateField, F
+ from django.db.models.functions import Cast
+
+ qs = MyModel.objects.annotate(
+ previous_day=Cast(
+ F("date_field") - datetime.timedelta(days=1),
+ output_field=DateField(),
+ )
+ )
+
+ Or on PostgreSQL only, use integer arithmetic to represent days::
+
+ from django.db.models import DateField, ExpressionWrapper, F
+
+ qs = MyModel.objects.annotate(
+ previous_day=ExpressionWrapper(
+ F("date_field") - 1, # Subtract 1 day as integer
+ output_field=DateField(),
+ )
+ )
+
``DateTimeField``
-----------------