summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2018-11-17 16:18:54 -0500
committerTim Graham <timograham@gmail.com>2018-11-17 19:27:53 -0500
commite62f6e0968eb19a44198211b5398c5738db454c5 (patch)
tree49372a7a8a3c459b78020d1d1d8f412b29eb5258
parent0cf85e6b074794ac91857aa097f0b3dc3e6d9468 (diff)
Fixed #29505 -- Removed SchemaEditor's calling of callable defaults.
Thanks Eugene Pakhomov for the suggested fix.
-rw-r--r--django/db/backends/base/schema.py9
-rw-r--r--tests/backends/base/test_schema.py22
2 files changed, 25 insertions, 6 deletions
diff --git a/django/db/backends/base/schema.py b/django/db/backends/base/schema.py
index f5cb433d6c..813c5bbd75 100644
--- a/django/db/backends/base/schema.py
+++ b/django/db/backends/base/schema.py
@@ -212,16 +212,13 @@ class BaseDatabaseSchemaEditor:
default = datetime.now()
internal_type = field.get_internal_type()
if internal_type == 'DateField':
- default = default.date
+ default = default.date()
elif internal_type == 'TimeField':
- default = default.time
+ default = default.time()
elif internal_type == 'DateTimeField':
- default = timezone.now
+ default = timezone.now()
else:
default = None
- # If it's a callable, call it
- if callable(default):
- default = default()
# Convert the value so it can be sent to the database.
return field.get_db_prep_save(default, self.connection)
diff --git a/tests/backends/base/test_schema.py b/tests/backends/base/test_schema.py
new file mode 100644
index 0000000000..9f740d9326
--- /dev/null
+++ b/tests/backends/base/test_schema.py
@@ -0,0 +1,22 @@
+from django.db import connection, models
+from django.test import TestCase
+
+
+class SchemaEditorTests(TestCase):
+
+ def test_effective_default_callable(self):
+ """SchemaEditor.effective_default() shouldn't call callable defaults."""
+ class MyStr(str):
+ def __call__(self):
+ return self
+
+ class MyCharField(models.CharField):
+ def _get_default(self):
+ return self.default
+
+ def get_db_prep_save(self, default, connection):
+ return default
+
+ field = MyCharField(max_length=1, default=MyStr)
+ with connection.schema_editor() as editor:
+ self.assertEqual(editor.effective_default(field), MyStr)