Django

Code

Changeset 3960

Show
Ignore:
Timestamp:
11/02/06 08:24:31 (2 years ago)
Author:
russellm
Message:

Fixes #2918 -- Clarified the db_prep_save logic for DateField? and DateTimeField? 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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/models/fields/__init__.py

    r3851 r3960  
    458458    def get_db_prep_save(self, value): 
    459459        # 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): 
    461463            value = value.strftime('%Y-%m-%d') 
    462464        return Field.get_db_prep_save(self, value) 
     
    488490    def get_db_prep_save(self, value): 
    489491        # Casts dates into string format for entry into database. 
    490         if value is not None
     492        if isinstance(value, datetime.datetime)
    491493            # MySQL will throw a warning if microseconds are given, because it 
    492494            # doesn't support microseconds. 
     
    494496                value = value.replace(microsecond=0) 
    495497            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             
    496505        return Field.get_db_prep_save(self, value) 
    497506