diff --git a/django/db/migrations/serializer.py b/django/db/migrations/serializer.py
index efda10e..92ff0b5 100644
a
|
b
|
import decimal
|
6 | 6 | import functools |
7 | 7 | import math |
8 | 8 | import types |
| 9 | import uuid |
9 | 10 | from importlib import import_module |
10 | 11 | |
11 | 12 | from django.db import models |
… |
… |
class TypeSerializer(BaseSerializer):
|
320 | 321 | return "%s.%s" % (module, self.value.__name__), {"import %s" % module} |
321 | 322 | |
322 | 323 | |
| 324 | class UUIDSerializer(BaseSerializer): |
| 325 | def serialize(self): |
| 326 | return "uuid.UUID('%s')" % self.value, {"import uuid"} |
| 327 | |
| 328 | |
323 | 329 | def serializer_factory(value): |
324 | 330 | from django.db.migrations.writer import SettingsReference |
325 | 331 | if isinstance(value, Promise): |
… |
… |
def serializer_factory(value):
|
382 | 388 | return IterableSerializer(value) |
383 | 389 | if isinstance(value, (COMPILED_REGEX_TYPE, RegexObject)): |
384 | 390 | return RegexSerializer(value) |
| 391 | if isinstance(value, uuid.UUID): |
| 392 | return UUIDSerializer(value) |
385 | 393 | raise ValueError( |
386 | 394 | "Cannot serialize: %r\nThere are some values Django cannot serialize into " |
387 | 395 | "migration files.\nFor more, see https://docs.djangoproject.com/en/%s/" |
diff --git a/docs/releases/1.11.txt b/docs/releases/1.11.txt
index a83bace..c78e032 100644
a
|
b
|
Management Commands
|
290 | 290 | Migrations |
291 | 291 | ~~~~~~~~~~ |
292 | 292 | |
293 | | * ... |
| 293 | * Added support for serialization of ``uuid.UUID`` objects. |
294 | 294 | |
295 | 295 | Models |
296 | 296 | ~~~~~~ |
diff --git a/docs/topics/migrations.txt b/docs/topics/migrations.txt
index 41d8727..a4e36b0 100644
a
|
b
|
Django can serialize the following:
|
669 | 669 | (include those that are timezone-aware) |
670 | 670 | - ``decimal.Decimal`` instances |
671 | 671 | - ``enum.Enum`` instances |
| 672 | - ``uuid.UUID`` instances |
672 | 673 | - ``functools.partial`` instances which have serializable ``func``, ``args``, |
673 | 674 | and ``keywords`` values. |
674 | 675 | - ``LazyObject`` instances which wrap a serializable value. |
… |
… |
Django can serialize the following:
|
681 | 682 | |
682 | 683 | Serialization support for ``enum.Enum`` was added. |
683 | 684 | |
| 685 | .. versionchanged:: 1.11 |
| 686 | |
| 687 | Serialization support for ``uuid.UUID`` was added. |
| 688 | |
684 | 689 | Django can serialize the following on Python 3 only: |
685 | 690 | |
686 | 691 | - Unbound methods used from within the class body (see below) |
diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py
index e2d85fe..3797b48 100644
a
|
b
|
import re
|
10 | 10 | import sys |
11 | 11 | import tokenize |
12 | 12 | import unittest |
| 13 | import uuid |
13 | 14 | |
14 | 15 | import custom_migration_operations.more_operations |
15 | 16 | import custom_migration_operations.operations |
… |
… |
class WriterTests(SimpleTestCase):
|
321 | 322 | "default=migrations.test_writer.IntEnum(1))" |
322 | 323 | ) |
323 | 324 | |
| 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 | |
324 | 348 | def test_serialize_functions(self): |
325 | 349 | with self.assertRaisesMessage(ValueError, 'Cannot serialize function: lambda'): |
326 | 350 | self.assertSerializedEqual(lambda x: 42) |