Field encoding

This commit is contained in:
Andrew Godwin 2013-06-07 15:36:31 +01:00
parent 4492f06408
commit c7aa4b5338
2 changed files with 22 additions and 1 deletions

View File

@ -1,5 +1,6 @@
import datetime
import types
from django.db import models
class MigrationWriter(object):
@ -85,6 +86,25 @@ class MigrationWriter(object):
# Simple types
elif isinstance(value, (int, long, float, str, unicode, bool, types.NoneType)):
return repr(value), set()
# Django fields
elif isinstance(value, models.Field):
attr_name, path, args, kwargs = value.deconstruct()
module, name = path.rsplit(".", 1)
if module == "django.db.models":
imports = set()
else:
imports = set("import %s" % module)
name = path
arg_strings = []
for arg in args:
arg_string, arg_imports = cls.serialize(arg)
arg_strings.append(arg_string)
imports.update(arg_imports)
for kw, arg in kwargs.items():
arg_string, arg_imports = cls.serialize(arg)
imports.update(arg_imports)
arg_strings.append("%s=%s" % (kw, arg_string))
return "%s(%s)" % (name, ", ".join(arg_strings)), imports
# Functions
elif isinstance(value, (types.FunctionType, types.BuiltinFunctionType)):
# Special-cases, as these don't have im_class

View File

@ -2,7 +2,7 @@
import datetime
from django.test import TransactionTestCase
from django.db.migrations.writer import MigrationWriter
from django.db import migrations
from django.db import models, migrations
class WriterTests(TransactionTestCase):
@ -56,6 +56,7 @@ class WriterTests(TransactionTestCase):
migration = type("Migration", (migrations.Migration,), {
"operations": [
migrations.DeleteModel("MyModel"),
migrations.AddField("OtherModel", "field_name", models.DateTimeField(default=datetime.datetime.utcnow))
],
"dependencies": [("testapp", "some_other_one")],
})