Field encoding
This commit is contained in:
parent
4492f06408
commit
c7aa4b5338
|
@ -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
|
||||
|
|
|
@ -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")],
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue