Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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 loic authored timgraham committed
Showing with 5 additions and 12 deletions.
  1. +3 −12 django/db/migrations/writer.py
  2. +2 −0  tests/migrations/test_writer.py
View
15 django/db/migrations/writer.py
@@ -148,18 +148,9 @@ def serialize(cls, value):
return cls.serialize_deconstructed(path, args, kwargs)
# Functions
elif isinstance(value, (types.FunctionType, types.BuiltinFunctionType)):
- # Special-cases, as these don't have im_class
- special_cases = [
- (datetime.datetime.now, "datetime.datetime.now", ["import datetime"]),
- (datetime.datetime.utcnow, "datetime.datetime.utcnow", ["import datetime"]),
- (datetime.date.today, "datetime.date.today", ["import datetime"]),
- ]
- for func, string, imports in special_cases:
- if func == value: # For some reason "utcnow is not utcnow"
- return string, set(imports)
- # Method?
- if hasattr(value, "im_class"):
- klass = value.im_class
+ # @classmethod?
+ if getattr(value, "__self__", None) and isinstance(value.__self__, type):
+ klass = value.__self__
module = klass.__module__
return "%s.%s.%s" % (module, klass.__name__, value.__name__), set(["import %s" % module])
elif hasattr(value, 'deconstruct'):
View
2  tests/migrations/test_writer.py
@@ -73,6 +73,8 @@ def test_serialize(self):
# Datetime stuff
self.assertSerializedEqual(datetime.datetime.utcnow())
self.assertSerializedEqual(datetime.datetime.utcnow)
+ self.assertSerializedEqual(datetime.datetime.today())
+ self.assertSerializedEqual(datetime.datetime.today)
self.assertSerializedEqual(datetime.date.today())
self.assertSerializedEqual(datetime.date.today)
# Django fields
Please sign in to comment.
Something went wrong with that request. Please try again.