Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixes #2918 -- Clarified the db_prep_save logic for DateField and Dat…

…eTimeField to prevent accidental conversion of non-datetime objects into strings, because SQLite doesn't appear to check for valid date format in a string used on an UPDATE of a datetime column.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3960 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 7f71ae1b8dcbd2c74f53e479577f165e178df93c 1 parent 6645d1f
Russell Keith-Magee authored November 02, 2006

Showing 1 changed file with 11 additions and 2 deletions. Show diff stats Hide diff stats

  1. 13  django/db/models/fields/__init__.py
13  django/db/models/fields/__init__.py
@@ -457,7 +457,9 @@ def get_follow(self, override=None):
457 457
 
458 458
     def get_db_prep_save(self, value):
459 459
         # Casts dates into string format for entry into database.
460  
-        if value is not None:
  460
+        if isinstance(value, datetime.datetime):
  461
+            value = value.date().strftime('%Y-%m-%d')
  462
+        elif isinstance(value, datetime.date):
461 463
             value = value.strftime('%Y-%m-%d')
462 464
         return Field.get_db_prep_save(self, value)
463 465
 
@@ -487,12 +489,19 @@ def to_python(self, value):
487 489
 
488 490
     def get_db_prep_save(self, value):
489 491
         # Casts dates into string format for entry into database.
490  
-        if value is not None:
  492
+        if isinstance(value, datetime.datetime):
491 493
             # MySQL will throw a warning if microseconds are given, because it
492 494
             # doesn't support microseconds.
493 495
             if settings.DATABASE_ENGINE == 'mysql' and hasattr(value, 'microsecond'):
494 496
                 value = value.replace(microsecond=0)
495 497
             value = str(value)
  498
+        elif isinstance(value, datetime.date):
  499
+            # MySQL will throw a warning if microseconds are given, because it
  500
+            # doesn't support microseconds.
  501
+            if settings.DATABASE_ENGINE == 'mysql' and hasattr(value, 'microsecond'):
  502
+                value = datetime.datetime(value.year, value.month, value.day, microsecond=0)
  503
+            value = str(value)
  504
+            
496 505
         return Field.get_db_prep_save(self, value)
497 506
 
498 507
     def get_db_prep_lookup(self, lookup_type, value):

0 notes on commit 7f71ae1

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