From 075da2250d31a6365c2cfb2f2da815b1880a4829 Mon Sep 17 00:00:00 2001 From: MokuLakshithReddy Date: Fri, 13 Nov 2026 17:30:03 +0530 Subject: [PATCH 1/4] fix(fields): store auto_now datetimes as UTC when use_tz=True (#2412) When use_tz=True and a custom timezone is configured, to_db_value() previously saved auto_now and auto_now_add values with the local timezone offset (timezone.localtime()). On backends like SQLite which compare timestamps lexicographically as text strings, storing local offsets broke time filtering and ordering against timezone.now() (UTC). docs/timezone.rst specifies that 'When enabled, all datetimes are stored as UTC in the database'. By using timezone.now() for the returned DB value, the database column always receives UTC while the Python model instance attribute continues to receive the localized timestamp via to_python_value(). Fixes #2412 --- CHANGELOG.rst | 1 + tests/fields/test_time.py | 23 +++++++++++++++++++++++ tortoise/fields/data.py | 2 +- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index bf2026941..b45246c3d 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -25,6 +25,7 @@ Changed Fixed ^^^^^ +- Store ``auto_now`` and ``auto_now_add`` datetime values as UTC when ``use_tz=True``, fixing SQLite time filtering and ordering with custom timezones. (#2412) - ``DecimalField`` no longer raises ``decimal.InvalidOperation`` for values with more than 28 significant digits. (#2349) - fix(expressions): inline subquery parameters when ``params_inline`` is enabled (#2340) - Use database column names in reverse foreign-key and one-to-one filters with renamed columns or one-to-one primary keys. (#2338) diff --git a/tests/fields/test_time.py b/tests/fields/test_time.py index 650164a52..beb0c6aad 100644 --- a/tests/fields/test_time.py +++ b/tests/fields/test_time.py @@ -923,9 +923,11 @@ async def test_datetime_naive_input_with_custom_timezone_update(db, enable_tz): ) started_at = timezone.localtime() + await sleep(0.01) obj.datetime = started_at.replace(tzinfo=None) with pytest.warns(RuntimeWarning, match="received a naive datetime"): await obj.save() + await sleep(0.01) now = timezone.localtime() obj = await model.get(id=obj.id) assert obj.datetime == started_at, f"Expected {started_at}, got {obj.datetime}" @@ -933,3 +935,24 @@ async def test_datetime_naive_input_with_custom_timezone_update(db, enable_tz): assert now > obj.datetime_auto > started_at, ( f"Expected: {now} > {obj.datetime_auto} > {started_at}" ) + + +@pytest.mark.asyncio +async def test_datetime_auto_now_with_custom_timezone_sqlite_filters(db, enable_tz): + """Test auto_now/auto_now_add with custom timezone allows correct SQLite time filters (#2412).""" + model = testmodels.DatetimeFields + for tz_name in ("UTC", "Asia/Kolkata", "America/New_York", "Asia/Shanghai"): + enable_tz(tz_name) + obj = await model.create(datetime=datetime(2021, 1, 1, tzinfo=get_default_timezone())) + now = timezone.now() + + # The row was just created, so created <= now and created > now - 1h + count_lte = await model.filter(id=obj.id, datetime_add__lte=now).count() + count_gt = await model.filter(id=obj.id, datetime_add__gt=now - timedelta(hours=1)).count() + assert count_lte == 1, f"Expected datetime_add__lte=now for {tz_name}, got {count_lte}" + assert count_gt == 1, f"Expected datetime_add__gt=now-1h for {tz_name}, got {count_gt}" + + # Fetch from DB and check instance matches + obj_db = await model.get(id=obj.id) + assert obj.datetime_add == obj_db.datetime_add + assert obj.datetime_add.tzinfo.key == tz_name or str(obj.datetime_add.tzinfo) == tz_name diff --git a/tortoise/fields/data.py b/tortoise/fields/data.py index 1cdf6cdd0..1294af1b8 100644 --- a/tortoise/fields/data.py +++ b/tortoise/fields/data.py @@ -538,7 +538,7 @@ def to_db_value( self.auto_now or (self.auto_now_add and getattr(instance, self.model_field_name) is None) ): - now = timezone.localtime() if get_use_tz() else timezone.now() + now = timezone.now() # Convert to match what would be read from DB (apply timezone conversion) now_python = self.to_python_value(now) setattr(instance, self.model_field_name, now_python) From 48557ba0f3673cae1d7f51eaeafe74b8496442ab Mon Sep 17 00:00:00 2001 From: Lakshith Reddy Date: Sat, 14 Nov 2026 20:50:16 +0530 Subject: [PATCH 2/4] fix(fields): read auto timestamps from UTC-backed columns correctly --- tortoise/fields/data.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tortoise/fields/data.py b/tortoise/fields/data.py index 1294af1b8..3f8eece1e 100644 --- a/tortoise/fields/data.py +++ b/tortoise/fields/data.py @@ -518,9 +518,17 @@ def to_python_value(self, value: Any) -> datetime.datetime | None: else parse_datetime(value) ) if get_use_tz(): - # When use_tz=True, ensure all datetimes are timezone-aware + # When use_tz=True, ensure all datetimes are timezone-aware. + # auto_now/auto_now_add values are written in UTC. Backends with + # timezone-naive timestamp columns return those values as naive + # datetimes, so interpret them as UTC before converting to the + # configured timezone. User-supplied naive datetimes retain the + # existing behavior of being interpreted in the configured zone. if timezone.is_naive(value): - value = timezone.make_aware(value) + if self.auto_now or self.auto_now_add: + value = timezone.make_aware(value, "UTC").astimezone(get_default_timezone()) + else: + value = timezone.make_aware(value) else: value = value.astimezone(get_default_timezone()) else: From 23a209223164290664bc141ce6c24e96bb7e05d4 Mon Sep 17 00:00:00 2001 From: Lakshith Reddy Date: Wed, 18 Nov 2026 01:12:02 +0530 Subject: [PATCH 3/4] fix(datetime): keep auto_now_add database values in UTC on updates --- tortoise/fields/data.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tortoise/fields/data.py b/tortoise/fields/data.py index 3f8eece1e..f80e8a96d 100644 --- a/tortoise/fields/data.py +++ b/tortoise/fields/data.py @@ -551,6 +551,18 @@ def to_db_value( now_python = self.to_python_value(now) setattr(instance, self.model_field_name, now_python) return now # type: ignore + # auto_now_add is initialized as a localized value on the model instance, + # but the database representation must remain UTC on later saves too. + # Otherwise a naive timestamp read back from MySQL/MSSQL is interpreted as + # UTC by to_python_value() and receives the timezone offset a second time. + if ( + self.auto_now_add + and value is not None + and isinstance(value, datetime.datetime) + and get_use_tz() + and timezone.is_aware(value) + ): + value = value.astimezone(datetime.timezone.utc) if ( value is not None and isinstance(value, datetime.datetime) From f5f51dc124300ae084f7c0947122b01852eb7235 Mon Sep 17 00:00:00 2001 From: MokuLakshithReddy Date: Fri, 20 Nov 2026 01:46:50 +0530 Subject: [PATCH 4/4] perf(fields): eliminate redundant datetime conversions and optimize to_db_value --- tortoise/fields/data.py | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/tortoise/fields/data.py b/tortoise/fields/data.py index f80e8a96d..95873ee9d 100644 --- a/tortoise/fields/data.py +++ b/tortoise/fields/data.py @@ -547,35 +547,26 @@ def to_db_value( or (self.auto_now_add and getattr(instance, self.model_field_name) is None) ): now = timezone.now() - # Convert to match what would be read from DB (apply timezone conversion) - now_python = self.to_python_value(now) - setattr(instance, self.model_field_name, now_python) + if get_use_tz(): + setattr(instance, self.model_field_name, now.astimezone(get_default_timezone())) + else: + setattr(instance, self.model_field_name, now) return now # type: ignore - # auto_now_add is initialized as a localized value on the model instance, - # but the database representation must remain UTC on later saves too. - # Otherwise a naive timestamp read back from MySQL/MSSQL is interpreted as - # UTC by to_python_value() and receives the timezone offset a second time. - if ( - self.auto_now_add - and value is not None - and isinstance(value, datetime.datetime) - and get_use_tz() - and timezone.is_aware(value) - ): - value = value.astimezone(datetime.timezone.utc) if ( value is not None and isinstance(value, datetime.datetime) and get_use_tz() - and timezone.is_naive(value) ): - warnings.warn( - f"DateTimeField {self.model_field_name} received a naive datetime ({value})" - " while time zone support is active.", - RuntimeWarning, - stacklevel=2, - ) - value = timezone.make_aware(value) # ty:ignore[invalid-assignment] + if timezone.is_aware(value): + value = value.astimezone(datetime.timezone.utc) + else: + warnings.warn( + f"DateTimeField {self.model_field_name} received a naive datetime ({value})" + " while time zone support is active.", + RuntimeWarning, + stacklevel=2, + ) + value = timezone.make_aware(value) # ty:ignore[invalid-assignment] self.validate(value) return value