Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added support for serializing class methods. - Refs #21290.

The new handling allows us to do away with the whitelisting that was
required to support date and datetime objects.
  • Loading branch information...
commit 8d6953d55c3aba04bbaf0f268499d6e405c653ff 1 parent 5841104
Loic Bistuer authored October 19, 2013 timgraham committed October 19, 2013
15  django/db/migrations/writer.py
@@ -148,18 +148,9 @@ def serialize(cls, value):
148 148
             return cls.serialize_deconstructed(path, args, kwargs)
149 149
         # Functions
150 150
         elif isinstance(value, (types.FunctionType, types.BuiltinFunctionType)):
151  
-            # Special-cases, as these don't have im_class
152  
-            special_cases = [
153  
-                (datetime.datetime.now, "datetime.datetime.now", ["import datetime"]),
154  
-                (datetime.datetime.utcnow, "datetime.datetime.utcnow", ["import datetime"]),
155  
-                (datetime.date.today, "datetime.date.today", ["import datetime"]),
156  
-            ]
157  
-            for func, string, imports in special_cases:
158  
-                if func == value:  # For some reason "utcnow is not utcnow"
159  
-                    return string, set(imports)
160  
-            # Method?
161  
-            if hasattr(value, "im_class"):
162  
-                klass = value.im_class
  151
+            # @classmethod?
  152
+            if getattr(value, "__self__", None) and isinstance(value.__self__, type):
  153
+                klass = value.__self__
163 154
                 module = klass.__module__
164 155
                 return "%s.%s.%s" % (module, klass.__name__, value.__name__), set(["import %s" % module])
165 156
             elif hasattr(value, 'deconstruct'):
2  tests/migrations/test_writer.py
@@ -73,6 +73,8 @@ def test_serialize(self):
73 73
         # Datetime stuff
74 74
         self.assertSerializedEqual(datetime.datetime.utcnow())
75 75
         self.assertSerializedEqual(datetime.datetime.utcnow)
  76
+        self.assertSerializedEqual(datetime.datetime.today())
  77
+        self.assertSerializedEqual(datetime.datetime.today)
76 78
         self.assertSerializedEqual(datetime.date.today())
77 79
         self.assertSerializedEqual(datetime.date.today)
78 80
         # Django fields

0 notes on commit 8d6953d

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