diff --git a/django/db/migrations/serializer.py b/django/db/migrations/serializer.py index 300bde3780..094ebb413a 100644 --- a/django/db/migrations/serializer.py +++ b/django/db/migrations/serializer.py @@ -10,6 +10,7 @@ import pathlib import re import types import uuid +import zoneinfo from django.conf import SettingsReference from django.db import models @@ -334,6 +335,11 @@ class UUIDSerializer(BaseSerializer): return "uuid.%s" % repr(self.value), {"import uuid"} +class ZoneInfoSerializer(BaseSerializer): + def serialize(self): + return repr(self.value), {"import zoneinfo"} + + class Serializer: _registry = { # Some of these are order-dependent. @@ -357,6 +363,7 @@ class Serializer: uuid.UUID: UUIDSerializer, pathlib.PurePath: PathSerializer, os.PathLike: PathLikeSerializer, + zoneinfo.ZoneInfo: ZoneInfoSerializer, } @classmethod diff --git a/docs/releases/6.0.txt b/docs/releases/6.0.txt index fe090e3f2a..538d242b98 100644 --- a/docs/releases/6.0.txt +++ b/docs/releases/6.0.txt @@ -178,6 +178,8 @@ Migrations * Squashed migrations can now themselves be squashed before being transitioned to normal migrations. +* Migrations now support serialization of :class:`zoneinfo.ZoneInfo` instances. + Models ~~~~~~ diff --git a/docs/topics/migrations.txt b/docs/topics/migrations.txt index 646b8d5116..799c7f19f4 100644 --- a/docs/topics/migrations.txt +++ b/docs/topics/migrations.txt @@ -778,6 +778,7 @@ Django can serialize the following: - ``list``, ``set``, ``tuple``, ``dict``, ``range``. - ``datetime.date``, ``datetime.time``, and ``datetime.datetime`` instances (include those that are timezone-aware) +- :class:`zoneinfo.ZoneInfo` instances - ``decimal.Decimal`` instances - ``enum.Enum`` and ``enum.Flag`` instances - ``uuid.UUID`` instances @@ -803,6 +804,10 @@ Django can serialize the following: - Any class reference (must be in module's top-level scope) - Anything with a custom ``deconstruct()`` method (:ref:`see below `) +.. versionchanged:: 6.0 + + Serialization support for :class:`zoneinfo.ZoneInfo` instances was added. + Django cannot serialize: - Nested classes diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py index 3a070f4d45..6e9355d9bc 100644 --- a/tests/migrations/test_writer.py +++ b/tests/migrations/test_writer.py @@ -612,6 +612,20 @@ class WriterTests(SimpleTestCase): string = MigrationWriter.serialize(field)[0] self.assertEqual(string, "models.FilePathField(path=%r)" % path_like.path) + def test_serialize_zoneinfo(self): + self.assertSerializedEqual(zoneinfo.ZoneInfo("Asia/Kolkata")) + self.assertSerializedResultEqual( + zoneinfo.ZoneInfo("Asia/Kolkata"), + ( + "zoneinfo.ZoneInfo(key='Asia/Kolkata')", + {"import zoneinfo"}, + ), + ) + self.assertSerializedResultEqual( + zoneinfo.ZoneInfo("Europe/Paris"), + ("zoneinfo.ZoneInfo(key='Europe/Paris')", {"import zoneinfo"}), + ) + def test_serialize_functions(self): with self.assertRaisesMessage(ValueError, "Cannot serialize function: lambda"): self.assertSerializedEqual(lambda x: 42)