Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Field encoding

  • Loading branch information...
commit c7aa4b5338e6bcf62f66eb53b309aa3a69b9a55d 1 parent 4492f06
Andrew Godwin authored June 07, 2013
20  django/db/migrations/writer.py
... ...
@@ -1,5 +1,6 @@
1 1
 import datetime
2 2
 import types
  3
+from django.db import models
3 4
 
4 5
 
5 6
 class MigrationWriter(object):
@@ -85,6 +86,25 @@ def serialize(cls, value):
85 86
         # Simple types
86 87
         elif isinstance(value, (int, long, float, str, unicode, bool, types.NoneType)):
87 88
             return repr(value), set()
  89
+        # Django fields
  90
+        elif isinstance(value, models.Field):
  91
+            attr_name, path, args, kwargs = value.deconstruct()
  92
+            module, name = path.rsplit(".", 1)
  93
+            if module == "django.db.models":
  94
+                imports = set()
  95
+            else:
  96
+                imports = set("import %s" % module)
  97
+                name = path
  98
+            arg_strings = []
  99
+            for arg in args:
  100
+                arg_string, arg_imports = cls.serialize(arg)
  101
+                arg_strings.append(arg_string)
  102
+                imports.update(arg_imports)
  103
+            for kw, arg in kwargs.items():
  104
+                arg_string, arg_imports = cls.serialize(arg)
  105
+                imports.update(arg_imports)
  106
+                arg_strings.append("%s=%s" % (kw, arg_string))
  107
+            return "%s(%s)" % (name, ", ".join(arg_strings)), imports
88 108
         # Functions
89 109
         elif isinstance(value, (types.FunctionType, types.BuiltinFunctionType)):
90 110
             # Special-cases, as these don't have im_class
3  tests/migrations/test_writer.py
@@ -2,7 +2,7 @@
2 2
 import datetime
3 3
 from django.test import TransactionTestCase
4 4
 from django.db.migrations.writer import MigrationWriter
5  
-from django.db import migrations
  5
+from django.db import models, migrations
6 6
 
7 7
 
8 8
 class WriterTests(TransactionTestCase):
@@ -56,6 +56,7 @@ def test_simple_migration(self):
56 56
         migration = type("Migration", (migrations.Migration,), {
57 57
             "operations": [
58 58
                 migrations.DeleteModel("MyModel"),
  59
+                migrations.AddField("OtherModel", "field_name", models.DateTimeField(default=datetime.datetime.utcnow))
59 60
             ],
60 61
             "dependencies": [("testapp", "some_other_one")],
61 62
         })

0 notes on commit c7aa4b5

Please sign in to comment.
Something went wrong with that request. Please try again.