Skip to content

Commit

Permalink
Fixes #2918 -- Clarified the db_prep_save logic for DateField and Dat…
Browse files Browse the repository at this point in the history
…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
freakboy3742 committed Nov 2, 2006
1 parent 6645d1f commit 7f71ae1
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions django/db/models/fields/__init__.py
Expand Up @@ -457,7 +457,9 @@ def get_follow(self, override=None):

def get_db_prep_save(self, value):
# Casts dates into string format for entry into database.
if value is not None:
if isinstance(value, datetime.datetime):
value = value.date().strftime('%Y-%m-%d')
elif isinstance(value, datetime.date):
value = value.strftime('%Y-%m-%d')
return Field.get_db_prep_save(self, value)

Expand Down Expand Up @@ -487,12 +489,19 @@ def to_python(self, value):

def get_db_prep_save(self, value):
# Casts dates into string format for entry into database.
if value is not None:
if isinstance(value, datetime.datetime):
# MySQL will throw a warning if microseconds are given, because it
# doesn't support microseconds.
if settings.DATABASE_ENGINE == 'mysql' and hasattr(value, 'microsecond'):
value = value.replace(microsecond=0)
value = str(value)
elif isinstance(value, datetime.date):
# MySQL will throw a warning if microseconds are given, because it
# doesn't support microseconds.
if settings.DATABASE_ENGINE == 'mysql' and hasattr(value, 'microsecond'):
value = datetime.datetime(value.year, value.month, value.day, microsecond=0)
value = str(value)

return Field.get_db_prep_save(self, value)

def get_db_prep_lookup(self, lookup_type, value):
Expand Down

0 comments on commit 7f71ae1

Please sign in to comment.