Ticket #27378: uudserializer.patch

File uudserializer.patch, 3.9 KB (added by Yuriy Korobko, 8 years ago)
  • django/db/migrations/serializer.py

    diff --git a/django/db/migrations/serializer.py b/django/db/migrations/serializer.py
    index efda10e..92ff0b5 100644
    a b import decimal  
    66import functools
    77import math
    88import types
     9import uuid
    910from importlib import import_module
    1011
    1112from django.db import models
    class TypeSerializer(BaseSerializer):  
    320321                return "%s.%s" % (module, self.value.__name__), {"import %s" % module}
    321322
    322323
     324class UUIDSerializer(BaseSerializer):
     325    def serialize(self):
     326        return "uuid.UUID('%s')" % self.value, {"import uuid"}
     327
     328
    323329def serializer_factory(value):
    324330    from django.db.migrations.writer import SettingsReference
    325331    if isinstance(value, Promise):
    def serializer_factory(value):  
    382388        return IterableSerializer(value)
    383389    if isinstance(value, (COMPILED_REGEX_TYPE, RegexObject)):
    384390        return RegexSerializer(value)
     391    if isinstance(value, uuid.UUID):
     392        return  UUIDSerializer(value)
    385393    raise ValueError(
    386394        "Cannot serialize: %r\nThere are some values Django cannot serialize into "
    387395        "migration files.\nFor more, see https://docs.djangoproject.com/en/%s/"
  • docs/releases/1.11.txt

    diff --git a/docs/releases/1.11.txt b/docs/releases/1.11.txt
    index a83bace..c78e032 100644
    a b Management Commands  
    290290Migrations
    291291~~~~~~~~~~
    292292
    293 * ...
     293* Added support for serialization of ``uuid.UUID`` objects.
    294294
    295295Models
    296296~~~~~~
  • docs/topics/migrations.txt

    diff --git a/docs/topics/migrations.txt b/docs/topics/migrations.txt
    index 41d8727..a4e36b0 100644
    a b Django can serialize the following:  
    669669  (include those that are timezone-aware)
    670670- ``decimal.Decimal`` instances
    671671- ``enum.Enum`` instances
     672- ``uuid.UUID`` instances
    672673- ``functools.partial`` instances which have serializable ``func``, ``args``,
    673674  and ``keywords`` values.
    674675- ``LazyObject`` instances which wrap a serializable value.
    Django can serialize the following:  
    681682
    682683    Serialization support for ``enum.Enum`` was added.
    683684
     685.. versionchanged:: 1.11
     686
     687    Serialization support for ``uuid.UUID`` was added.
     688
    684689Django can serialize the following on Python 3 only:
    685690
    686691- Unbound methods used from within the class body (see below)
  • tests/migrations/test_writer.py

    diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py
    index e2d85fe..3797b48 100644
    a b import re  
    1010import sys
    1111import tokenize
    1212import unittest
     13import uuid
    1314
    1415import custom_migration_operations.more_operations
    1516import custom_migration_operations.operations
    class WriterTests(SimpleTestCase):  
    321322            "default=migrations.test_writer.IntEnum(1))"
    322323        )
    323324
     325    def test_serialize_uuid(self):
     326        uuid_a = uuid.UUID('5c859437-d061-4847-b3f7-e6b78852f8c8')
     327        uuid_b = uuid.UUID('c7853ec1-2ea3-4359-b02d-b54e8f1bcee2')
     328
     329        self.assertSerializedResultEqual(
     330            uuid_a,
     331            ("uuid.UUID('" + str(uuid_a) + "')", {'import uuid'})
     332        )
     333        self.assertSerializedResultEqual(
     334            uuid_b,
     335            ("uuid.UUID('" + str(uuid_b) + "')", {'import uuid'})
     336        )
     337
     338        field = models.UUIDField(choices=((uuid_a, 'UUID A'), (uuid_b, 'UUID B')), default=uuid_a)
     339        string = MigrationWriter.serialize(field)[0]
     340        self.assertEqual(
     341            string,
     342            "models.UUIDField(choices=["
     343            "(uuid.UUID('5c859437-d061-4847-b3f7-e6b78852f8c8'), 'UUID A'), "
     344            "(uuid.UUID('c7853ec1-2ea3-4359-b02d-b54e8f1bcee2'), 'UUID B')], "
     345            "default=uuid.UUID('5c859437-d061-4847-b3f7-e6b78852f8c8'))"
     346        )
     347
    324348    def test_serialize_functions(self):
    325349        with self.assertRaisesMessage(ValueError, 'Cannot serialize function: lambda'):
    326350            self.assertSerializedEqual(lambda x: 42)
Back to Top