Changes between Initial Version and Version 2 of Ticket #28715
- Timestamp:
- Oct 23, 2017, 5:39:11 AM (7 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #28715
- Property Triage Stage Unreviewed → Accepted
- Property Summary Migration DateTimeField(auto_now_add=True -> default=django.utils.timezone.new) → Prevent a migration changing DateTimeField(auto_now_add=True) to default=timezone.now from generating SQL
- Property Type Uncategorized → Cleanup/optimization
-
Ticket #28715 – Description
initial v2 2 2 ALTER TABLE SET DEFAULT '2017-10-16T09:35:52.710695'::timestamp; 3 3 ALTER TABLE DROP DEFAULT 4 which have no effects, apart from locking the whole table .4 which have no effects, apart from locking the whole table twice. 5 5 6 A proposal to recognize, when the effective default-callable doesn't change :6 A proposal to recognize, when the effective default-callable doesn't change and skip changing the DEFAULT twice in this case, as well as not generating a migration when this is the only change on a field: 7 7 {{{ 8 8 diff --git a/django/db/backends/base/schema.py b/django/db/backends/base/schema.py 9 9 --- a/django/db/backends/base/schema.py 10 10 +++ b/django/db/backends/base/schema.py 11 @@ -199,28 +199,3 2@@ class BaseDatabaseSchemaEditor(object):11 @@ -199,28 +199,33 @@ class BaseDatabaseSchemaEditor(object): 12 12 'requires_literal_defaults must provide a prepare_default() method' 13 13 ) 14 14 15 15 - def effective_default(self, field): 16 + def effective_default_before_callable(self, field): 16 + @staticmethod 17 + def effective_default_before_callable(field): 17 18 """ 18 19 - Returns a field's effective database default value … … 48 49 + Returns a field's effective database default value 49 50 + """ 50 + default = self.effective_default_before_callable(field)51 + default = BaseDatabaseSchemaEditor.effective_default_before_callable(field) 51 52 # If it's a callable, call it 52 53 if callable(default): 53 54 default = default() 54 @@ -615,6 +6 19,7 @@ class BaseDatabaseSchemaEditor(object):55 @@ -615,6 +620,7 @@ class BaseDatabaseSchemaEditor(object): 55 56 old_default != new_default and 56 57 new_default is not None and 57 58 not self.skip_default(new_field) 58 + and self.effective_default_before_callable(old_field) != self.effective_default_before_callable(new59 + and BaseDatabaseSchemaEditor.effective_default_before_callable(old_field) != BaseDatabaseSchemaEdit 59 60 ) 60 61 if needs_database_default: 61 62 if self.connection.features.requires_literal_defaults: 62 63 diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py 64 --- a/django/db/models/fields/__init__.py 65 +++ b/django/db/models/fields/__init__.py 66 @@ -1232,7 +1232,7 @@ class DateField(DateTimeCheckMixin, Field): 67 if self.auto_now: 68 kwargs['auto_now'] = True 69 if self.auto_now_add: 70 - kwargs['auto_now_add'] = True 71 + kwargs['default'] = timezone.now 72 if self.auto_now or self.auto_now_add: 73 del kwargs['editable'] 74 del kwargs['blank'] 63 75 }}}