From 542e74947535b10684e9e396a0d54979e9f70fbf Mon Sep 17 00:00:00 2001 From: Chris Jerdonek Date: Wed, 28 Jul 2021 02:45:32 -0400 Subject: [PATCH] Fixed #32966 -- Fixed TimeField.check() crash for timezone-aware times in default when USE_TZ = True. --- django/db/models/fields/__init__.py | 2 +- .../test_ordinary_fields.py | 32 +++++++++++++------ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 7198d430cea..d61c446d5ac 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -2213,7 +2213,7 @@ class TimeField(DateTimeCheckMixin, Field): upper = now + second_offset value = datetime.datetime.combine(now.date(), value) if timezone.is_aware(value): - value = timezone.make_naive(value, timezone.utc).time() + value = timezone.make_naive(value, timezone.utc) else: # No explicit time / datetime value -- no checks necessary return [] diff --git a/tests/invalid_models_tests/test_ordinary_fields.py b/tests/invalid_models_tests/test_ordinary_fields.py index 6eddd853af5..7797b242020 100644 --- a/tests/invalid_models_tests/test_ordinary_fields.py +++ b/tests/invalid_models_tests/test_ordinary_fields.py @@ -747,14 +747,16 @@ class TimeFieldTests(SimpleTestCase): class Model(models.Model): field_dt = models.TimeField(default=now()) field_t = models.TimeField(default=now().time()) + # Timezone-aware time object (when USE_TZ=True). + field_tz = models.TimeField(default=now().timetz()) field_now = models.DateField(default=now) - field_dt = Model._meta.get_field('field_dt') - field_t = Model._meta.get_field('field_t') - field_now = Model._meta.get_field('field_now') - errors = field_dt.check() - errors.extend(field_t.check()) - errors.extend(field_now.check()) # doesn't raise a warning + names = ['field_dt', 'field_t', 'field_tz', 'field_now'] + fields = [Model._meta.get_field(name) for name in names] + errors = [] + for field in fields: + errors.extend(field.check()) + self.assertEqual(errors, [ DjangoWarning( 'Fixed default value provided.', @@ -762,7 +764,7 @@ class TimeFieldTests(SimpleTestCase): 'value as default for this field. This may not be ' 'what you want. If you want to have the current date ' 'as default, use `django.utils.timezone.now`', - obj=field_dt, + obj=fields[0], id='fields.W161', ), DjangoWarning( @@ -771,9 +773,21 @@ class TimeFieldTests(SimpleTestCase): 'value as default for this field. This may not be ' 'what you want. If you want to have the current date ' 'as default, use `django.utils.timezone.now`', - obj=field_t, + obj=fields[1], id='fields.W161', - ) + ), + DjangoWarning( + 'Fixed default value provided.', + hint=( + 'It seems you set a fixed date / time / datetime value as ' + 'default for this field. This may not be what you want. ' + 'If you want to have the current date as default, use ' + '`django.utils.timezone.now`' + ), + obj=fields[2], + id='fields.W161', + ), + # field_now doesn't raise a warning. ]) @override_settings(USE_TZ=True)