mirror of
https://github.com/django/django.git
synced 2024-12-22 17:16:24 +00:00
Fixed #32966 -- Fixed TimeField.check() crash for timezone-aware times in default when USE_TZ = True.
This commit is contained in:
parent
40d3cec22d
commit
542e749475
@ -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 []
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user