Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #17580 -- Made sure datetime.date instances are correctly handl…

…ed 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
  • Loading branch information...
commit 8f168976d68282a6180d2af4e12c4f3b6dd09c85 1 parent 6ae393d
@jezdez jezdez authored
View
6 django/db/models/fields/__init__.py
@@ -748,7 +748,11 @@ def to_python(self, value):
if isinstance(value, datetime.datetime):
return value
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)
View
7 tests/modeltests/timezones/tests.py
@@ -262,6 +262,13 @@ def test_naive_datetime(self):
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
@skipIf(sys.version_info < (2, 6), "this test requires Python >= 2.6")
@skipUnlessDBFeature('supports_microsecond_precision')
def test_naive_datetime_with_microsecond(self):
Please sign in to comment.
Something went wrong with that request. Please try again.