mirror of
https://github.com/django/django.git
synced 2024-12-23 01:25:58 +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
|
upper = now + second_offset
|
||||||
value = datetime.datetime.combine(now.date(), value)
|
value = datetime.datetime.combine(now.date(), value)
|
||||||
if timezone.is_aware(value):
|
if timezone.is_aware(value):
|
||||||
value = timezone.make_naive(value, timezone.utc).time()
|
value = timezone.make_naive(value, timezone.utc)
|
||||||
else:
|
else:
|
||||||
# No explicit time / datetime value -- no checks necessary
|
# No explicit time / datetime value -- no checks necessary
|
||||||
return []
|
return []
|
||||||
|
@ -747,14 +747,16 @@ class TimeFieldTests(SimpleTestCase):
|
|||||||
class Model(models.Model):
|
class Model(models.Model):
|
||||||
field_dt = models.TimeField(default=now())
|
field_dt = models.TimeField(default=now())
|
||||||
field_t = models.TimeField(default=now().time())
|
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_now = models.DateField(default=now)
|
||||||
|
|
||||||
field_dt = Model._meta.get_field('field_dt')
|
names = ['field_dt', 'field_t', 'field_tz', 'field_now']
|
||||||
field_t = Model._meta.get_field('field_t')
|
fields = [Model._meta.get_field(name) for name in names]
|
||||||
field_now = Model._meta.get_field('field_now')
|
errors = []
|
||||||
errors = field_dt.check()
|
for field in fields:
|
||||||
errors.extend(field_t.check())
|
errors.extend(field.check())
|
||||||
errors.extend(field_now.check()) # doesn't raise a warning
|
|
||||||
self.assertEqual(errors, [
|
self.assertEqual(errors, [
|
||||||
DjangoWarning(
|
DjangoWarning(
|
||||||
'Fixed default value provided.',
|
'Fixed default value provided.',
|
||||||
@ -762,7 +764,7 @@ class TimeFieldTests(SimpleTestCase):
|
|||||||
'value as default for this field. This may not be '
|
'value as default for this field. This may not be '
|
||||||
'what you want. If you want to have the current date '
|
'what you want. If you want to have the current date '
|
||||||
'as default, use `django.utils.timezone.now`',
|
'as default, use `django.utils.timezone.now`',
|
||||||
obj=field_dt,
|
obj=fields[0],
|
||||||
id='fields.W161',
|
id='fields.W161',
|
||||||
),
|
),
|
||||||
DjangoWarning(
|
DjangoWarning(
|
||||||
@ -771,9 +773,21 @@ class TimeFieldTests(SimpleTestCase):
|
|||||||
'value as default for this field. This may not be '
|
'value as default for this field. This may not be '
|
||||||
'what you want. If you want to have the current date '
|
'what you want. If you want to have the current date '
|
||||||
'as default, use `django.utils.timezone.now`',
|
'as default, use `django.utils.timezone.now`',
|
||||||
obj=field_t,
|
obj=fields[1],
|
||||||
id='fields.W161',
|
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)
|
@override_settings(USE_TZ=True)
|
||||||
|
Loading…
Reference in New Issue
Block a user