Fixed #21852 -- Make migration writer serialize iterators
This commit is contained in:
parent
088cb711fe
commit
047394f79c
|
@ -3,6 +3,7 @@ from __future__ import unicode_literals
|
||||||
import datetime
|
import datetime
|
||||||
import inspect
|
import inspect
|
||||||
import decimal
|
import decimal
|
||||||
|
import collections
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
import os
|
import os
|
||||||
import types
|
import types
|
||||||
|
@ -257,6 +258,16 @@ class MigrationWriter(object):
|
||||||
if hasattr(value, "__module__"):
|
if hasattr(value, "__module__"):
|
||||||
module = value.__module__
|
module = value.__module__
|
||||||
return "%s.%s" % (module, value.__name__), set(["import %s" % module])
|
return "%s.%s" % (module, value.__name__), set(["import %s" % module])
|
||||||
|
# Other iterables
|
||||||
|
elif isinstance(value, collections.Iterable):
|
||||||
|
imports = set()
|
||||||
|
strings = []
|
||||||
|
for item in value:
|
||||||
|
item_string, item_imports = cls.serialize(item)
|
||||||
|
imports.update(item_imports)
|
||||||
|
strings.append(item_string)
|
||||||
|
format = "(%s)" if len(strings) > 1 else "(%s,)"
|
||||||
|
return format % (", ".join(strings)), imports
|
||||||
# Uh oh.
|
# Uh oh.
|
||||||
else:
|
else:
|
||||||
raise ValueError("Cannot serialize: %r\nThere are some values Django cannot serialize into migration files.\nFor more, see https://docs.djangoproject.com/en/dev/topics/migrations/#migration-serializing" % value)
|
raise ValueError("Cannot serialize: %r\nThere are some values Django cannot serialize into migration files.\nFor more, see https://docs.djangoproject.com/en/dev/topics/migrations/#migration-serializing" % value)
|
||||||
|
|
|
@ -102,6 +102,13 @@ class WriterTests(TestCase):
|
||||||
set(["from django.conf import settings"]),
|
set(["from django.conf import settings"]),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
self.assertSerializedResultEqual(
|
||||||
|
((x, x*x) for x in range(3)),
|
||||||
|
(
|
||||||
|
"((0, 0), (1, 1), (2, 4))",
|
||||||
|
set(),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
def test_simple_migration(self):
|
def test_simple_migration(self):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue