Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #21852 -- Make migration writer serialize iterators

  • Loading branch information...
commit 047394f79c2cbc6cf44684a1f0d17aba1ddfe1c8 1 parent 088cb71
@MarkusH MarkusH authored
Showing with 18 additions and 0 deletions.
  1. +11 −0 django/db/migrations/writer.py
  2. +7 −0 tests/migrations/test_writer.py
View
11 django/db/migrations/writer.py
@@ -3,6 +3,7 @@
import datetime
import inspect
import decimal
+import collections
from importlib import import_module
import os
import types
@@ -257,6 +258,16 @@ def serialize(cls, value):
if hasattr(value, "__module__"):
module = value.__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.
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)
View
7 tests/migrations/test_writer.py
@@ -102,6 +102,13 @@ def test_serialize(self):
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):
"""
Please sign in to comment.
Something went wrong with that request. Please try again.