Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #17294 -- Supported nullable DateTimeFields when time zone supp…

…ort is enabled. Thanks pressureman for the report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17148 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 866c229f52df29f6b7b07463548532aae421fe42 1 parent e954a03
Aymeric Augustin authored November 25, 2011
2  django/db/models/fields/__init__.py
@@ -786,7 +786,7 @@ def pre_save(self, model_instance, add):
786 786
 
787 787
     def get_prep_value(self, value):
788 788
         value = self.to_python(value)
789  
-        if settings.USE_TZ and timezone.is_naive(value):
  789
+        if value is not None and settings.USE_TZ and timezone.is_naive(value):
790 790
             # For backwards compatibility, interpret naive datetimes in local
791 791
             # time. This won't work during DST change, but we can't do much
792 792
             # about it, so we let the exceptions percolate up the call stack.
3  django/utils/timezone.py
@@ -228,6 +228,9 @@ def now():
228 228
     else:
229 229
         return datetime.now()
230 230
 
  231
+# By design, these four functions don't perform any checks on their arguments.
  232
+# The caller should ensure that they don't receive an invalid value like None.
  233
+
231 234
 def is_aware(value):
232 235
     """
233 236
     Determines if a given datetime.datetime is aware.
3  tests/modeltests/timezones/models.py
@@ -3,6 +3,9 @@
3 3
 class Event(models.Model):
4 4
     dt = models.DateTimeField()
5 5
 
  6
+class MaybeEvent(models.Model):
  7
+    dt = models.DateTimeField(blank=True, null=True)
  8
+
6 9
 class Timestamp(models.Model):
7 10
     created = models.DateTimeField(auto_now_add=True)
8 11
     updated = models.DateTimeField(auto_now=True)
7  tests/modeltests/timezones/tests.py
@@ -25,7 +25,7 @@
25 25
 from django.utils.unittest import skipIf
26 26
 
27 27
 from .forms import EventForm, EventSplitForm, EventModelForm
28  
-from .models import Event, Timestamp
  28
+from .models import Event, MaybeEvent, Timestamp
29 29
 
30 30
 
31 31
 # These tests use the EAT (Eastern Africa Time) and ICT (Indochina Time)
@@ -403,6 +403,11 @@ def test_query_dates(self):
403 403
                  datetime.datetime(2011, 1, 1, tzinfo=UTC)],
404 404
                 transform=lambda d: d)
405 405
 
  406
+    def test_null_datetime(self):
  407
+        # Regression for #17294
  408
+        e = MaybeEvent.objects.create()
  409
+        self.assertEqual(e.dt, None)
  410
+
406 411
 NewDatabaseTests = override_settings(USE_TZ=True)(NewDatabaseTests)
407 412
 
408 413
 

0 notes on commit 866c229

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