Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #3172: Model.validate() no longer raises TypeErrors on empty Da…

…te*Fields. Thanks, floguy@gmail.com.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4592 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 6bd07383c0b06790c209de365e8e17ae125f490b 1 parent b7fa37f
Jacob Kaplan-Moss authored February 26, 2007
4  django/db/models/fields/__init__.py
@@ -443,6 +443,8 @@ def __init__(self, verbose_name=None, name=None, auto_now=False, auto_now_add=Fa
443 443
         Field.__init__(self, verbose_name, name, **kwargs)
444 444
 
445 445
     def to_python(self, value):
  446
+        if value is None:
  447
+            return value
446 448
         if isinstance(value, datetime.datetime):
447 449
             return value.date()
448 450
         if isinstance(value, datetime.date):
@@ -505,6 +507,8 @@ def formfield(self, **kwargs):
505 507
 
506 508
 class DateTimeField(DateField):
507 509
     def to_python(self, value):
  510
+        if value is None:
  511
+            return value
508 512
         if isinstance(value, datetime.datetime):
509 513
             return value
510 514
         if isinstance(value, datetime.date):
4  tests/modeltests/validation/models.py
@@ -146,4 +146,8 @@ def __str__(self):
146 146
 >>> p.validate()
147 147
 {'email': ['Enter a valid e-mail address.']}
148 148
 
  149
+# Make sure that Date and DateTime return validation errors and don't raise Python errors.
  150
+>>> Person(name='John Doe', is_child=True, email='abc@def.com').validate()
  151
+{'favorite_moment': ['This field is required.'], 'birthdate': ['This field is required.']}
  152
+
149 153
 """}

0 notes on commit 6bd0738

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