mirror of
https://github.com/django/django.git
synced 2025-06-05 11:39:13 +00:00
Fixed #36265 -- Added support for serialization of ZoneInfo instances in migrations.
This commit is contained in:
parent
02a5cbfe76
commit
126417be43
@ -10,6 +10,7 @@ import pathlib
|
|||||||
import re
|
import re
|
||||||
import types
|
import types
|
||||||
import uuid
|
import uuid
|
||||||
|
import zoneinfo
|
||||||
|
|
||||||
from django.conf import SettingsReference
|
from django.conf import SettingsReference
|
||||||
from django.db import models
|
from django.db import models
|
||||||
@ -334,6 +335,11 @@ class UUIDSerializer(BaseSerializer):
|
|||||||
return "uuid.%s" % repr(self.value), {"import uuid"}
|
return "uuid.%s" % repr(self.value), {"import uuid"}
|
||||||
|
|
||||||
|
|
||||||
|
class ZoneInfoSerializer(BaseSerializer):
|
||||||
|
def serialize(self):
|
||||||
|
return repr(self.value), {"import zoneinfo"}
|
||||||
|
|
||||||
|
|
||||||
class Serializer:
|
class Serializer:
|
||||||
_registry = {
|
_registry = {
|
||||||
# Some of these are order-dependent.
|
# Some of these are order-dependent.
|
||||||
@ -357,6 +363,7 @@ class Serializer:
|
|||||||
uuid.UUID: UUIDSerializer,
|
uuid.UUID: UUIDSerializer,
|
||||||
pathlib.PurePath: PathSerializer,
|
pathlib.PurePath: PathSerializer,
|
||||||
os.PathLike: PathLikeSerializer,
|
os.PathLike: PathLikeSerializer,
|
||||||
|
zoneinfo.ZoneInfo: ZoneInfoSerializer,
|
||||||
}
|
}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
@ -178,6 +178,8 @@ Migrations
|
|||||||
* Squashed migrations can now themselves be squashed before being transitioned
|
* Squashed migrations can now themselves be squashed before being transitioned
|
||||||
to normal migrations.
|
to normal migrations.
|
||||||
|
|
||||||
|
* Migrations now support serialization of :class:`zoneinfo.ZoneInfo` instances.
|
||||||
|
|
||||||
Models
|
Models
|
||||||
~~~~~~
|
~~~~~~
|
||||||
|
|
||||||
|
@ -778,6 +778,7 @@ Django can serialize the following:
|
|||||||
- ``list``, ``set``, ``tuple``, ``dict``, ``range``.
|
- ``list``, ``set``, ``tuple``, ``dict``, ``range``.
|
||||||
- ``datetime.date``, ``datetime.time``, and ``datetime.datetime`` instances
|
- ``datetime.date``, ``datetime.time``, and ``datetime.datetime`` instances
|
||||||
(include those that are timezone-aware)
|
(include those that are timezone-aware)
|
||||||
|
- :class:`zoneinfo.ZoneInfo` instances
|
||||||
- ``decimal.Decimal`` instances
|
- ``decimal.Decimal`` instances
|
||||||
- ``enum.Enum`` and ``enum.Flag`` instances
|
- ``enum.Enum`` and ``enum.Flag`` instances
|
||||||
- ``uuid.UUID`` instances
|
- ``uuid.UUID`` instances
|
||||||
@ -803,6 +804,10 @@ Django can serialize the following:
|
|||||||
- Any class reference (must be in module's top-level scope)
|
- Any class reference (must be in module's top-level scope)
|
||||||
- Anything with a custom ``deconstruct()`` method (:ref:`see below <custom-deconstruct-method>`)
|
- 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:
|
Django cannot serialize:
|
||||||
|
|
||||||
- Nested classes
|
- Nested classes
|
||||||
|
@ -612,6 +612,20 @@ class WriterTests(SimpleTestCase):
|
|||||||
string = MigrationWriter.serialize(field)[0]
|
string = MigrationWriter.serialize(field)[0]
|
||||||
self.assertEqual(string, "models.FilePathField(path=%r)" % path_like.path)
|
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):
|
def test_serialize_functions(self):
|
||||||
with self.assertRaisesMessage(ValueError, "Cannot serialize function: lambda"):
|
with self.assertRaisesMessage(ValueError, "Cannot serialize function: lambda"):
|
||||||
self.assertSerializedEqual(lambda x: 42)
|
self.assertSerializedEqual(lambda x: 42)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user