Optimized BaseDatabaseSchemaEditor._effective_default() a bit,

This eliminates an unneeded call to datetime.now() when
field.get_internal_type() equals "DateTimeField".
This commit is contained in:
Chris Jerdonek 2021-07-27 23:12:01 -07:00 committed by GitHub
parent fe074c96a3
commit 5fee36973c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 6 deletions

View File

@ -316,14 +316,15 @@ class BaseDatabaseSchemaEditor:
else:
default = ''
elif getattr(field, 'auto_now', False) or getattr(field, 'auto_now_add', False):
default = datetime.now()
internal_type = field.get_internal_type()
if internal_type == 'DateField':
default = default.date()
elif internal_type == 'TimeField':
default = default.time()
elif internal_type == 'DateTimeField':
if internal_type == 'DateTimeField':
default = timezone.now()
else:
default = datetime.now()
if internal_type == 'DateField':
default = default.date()
elif internal_type == 'TimeField':
default = default.time()
else:
default = None
return default