Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Field encoding

  • Loading branch information...
commit c7aa4b5338e6bcf62f66eb53b309aa3a69b9a55d 1 parent 4492f06
@andrewgodwin andrewgodwin authored
Showing with 22 additions and 1 deletion.
  1. +20 −0 django/db/migrations/writer.py
  2. +2 −1  tests/migrations/test_writer.py
View
20 django/db/migrations/writer.py
@@ -1,5 +1,6 @@
import datetime
import types
+from django.db import models
class MigrationWriter(object):
@@ -85,6 +86,25 @@ def serialize(cls, value):
# 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
3  tests/migrations/test_writer.py
@@ -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 @@ def test_simple_migration(self):
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")],
})
Please sign in to comment.
Something went wrong with that request. Please try again.