Fixed #17580 -- Made sure datetime.date instances are correctly handled when being passed to a DateTimeField fields and the timezone support is enabled by making it aware using the default timezone.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17392 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel 2012-01-24 10:00:39 +00:00
parent 6ae393d74d
commit 8f168976d6
2 changed files with 12 additions and 1 deletions

View File

@ -748,7 +748,11 @@ class DateTimeField(DateField):
if isinstance(value, datetime.datetime): if isinstance(value, datetime.datetime):
return value return value
if isinstance(value, datetime.date): if isinstance(value, datetime.date):
return datetime.datetime(value.year, value.month, value.day) value = datetime.datetime(value.year, value.month, value.day)
if settings.USE_TZ:
default_timezone = timezone.get_default_timezone()
value = timezone.make_aware(value, default_timezone)
return value
value = smart_str(value) value = smart_str(value)

View File

@ -261,6 +261,13 @@ class NewDatabaseTests(BaseDateTimeTests):
# naive datetimes are interpreted in local time # naive datetimes are interpreted in local time
self.assertEqual(event.dt, dt.replace(tzinfo=EAT)) self.assertEqual(event.dt, dt.replace(tzinfo=EAT))
@requires_tz_support
def test_datetime_from_date(self):
dt = datetime.date(2011, 9, 1)
Event.objects.create(dt=dt)
event = Event.objects.get()
self.assertEqual(event.dt, datetime.datetime(2011, 9, 1, tzinfo=EAT))
@requires_tz_support @requires_tz_support
@skipIf(sys.version_info < (2, 6), "this test requires Python >= 2.6") @skipIf(sys.version_info < (2, 6), "this test requires Python >= 2.6")
@skipUnlessDBFeature('supports_microsecond_precision') @skipUnlessDBFeature('supports_microsecond_precision')