Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Jannis Leidel authored January 24, 2012
6  django/db/models/fields/__init__.py
@@ -748,7 +748,11 @@ def to_python(self, value):
748 748
         if isinstance(value, datetime.datetime):
749 749
             return value
750 750
         if isinstance(value, datetime.date):
751  
-            return datetime.datetime(value.year, value.month, value.day)
  751
+            value = datetime.datetime(value.year, value.month, value.day)
  752
+            if settings.USE_TZ:
  753
+                default_timezone = timezone.get_default_timezone()
  754
+                value = timezone.make_aware(value, default_timezone)
  755
+            return value
752 756
 
753 757
         value = smart_str(value)
754 758
 
7  tests/modeltests/timezones/tests.py
@@ -262,6 +262,13 @@ def test_naive_datetime(self):
262 262
         self.assertEqual(event.dt, dt.replace(tzinfo=EAT))
263 263
 
264 264
     @requires_tz_support
  265
+    def test_datetime_from_date(self):
  266
+        dt = datetime.date(2011, 9, 1)
  267
+        Event.objects.create(dt=dt)
  268
+        event = Event.objects.get()
  269
+        self.assertEqual(event.dt, datetime.datetime(2011, 9, 1, tzinfo=EAT))
  270
+
  271
+    @requires_tz_support
265 272
     @skipIf(sys.version_info < (2, 6), "this test requires Python >= 2.6")
266 273
     @skipUnlessDBFeature('supports_microsecond_precision')
267 274
     def test_naive_datetime_with_microsecond(self):

0 notes on commit 8f16897

Please sign in to comment.
Something went wrong with that request. Please try again.