1
0
mirror of https://github.com/django/django.git synced 2025-04-04 21:46:40 +00:00

Fixed #36265 -- Added support for serialization of ZoneInfo instances in migrations.

This commit is contained in:
Song Junho 2025-03-29 18:49:12 +09:00 committed by Sarah Boyce
parent 02a5cbfe76
commit 126417be43
4 changed files with 28 additions and 0 deletions

View File

@ -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

View File

@ -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
~~~~~~

View File

@ -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 <custom-deconstruct-method>`)
.. versionchanged:: 6.0
Serialization support for :class:`zoneinfo.ZoneInfo` instances was added.
Django cannot serialize:
- Nested classes

View File

@ -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)