Code

Ticket #11984: django_bug_7383_v1.patch

File django_bug_7383_v1.patch, 3.2 KB (added by sorin, 4 years ago)
  • contrib/admin/templatetags/admin_list.py

     
    310310            } 
    311311        else: 
    312312            years = cl.query_set.dates(field_name, 'year') 
    313             return { 
     313            # this list can contain None objects so we need to remove them 
     314            result = { 
    314315                'show': True, 
    315                 'choices': [{ 
     316                'choices': [] 
     317            } 
     318            for year in years: 
     319                if year: 
     320                    result['choices'].append({ 
    316321                    'link': link({year_field: year.year}), 
    317322                    'title': year.year 
    318                 } for year in years] 
    319             } 
     323                }) 
     324            return result 
     325             
    320326date_hierarchy = register.inclusion_tag('admin/date_hierarchy.html')(date_hierarchy) 
    321327 
    322328def search_form(cl): 
  • db/backends/mysql/base.py

     
    134134            sql = field_name 
    135135        else: 
    136136            format_str = ''.join([f for f in format[:i]] + [f for f in format_def[i:]]) 
    137             sql = "CAST(DATE_FORMAT(%s, '%s') AS DATETIME)" % (field_name, format_str) 
     137            # do not use CAST(... as DATETIME) because it will fail if you have dates like 0000-00-00 00:00:00 
     138            # MySQL message: "Truncated incorrect datetime value: '0000-01-01 00:00:00'" ErrorNr: 1292 
     139            sql = "DATE_FORMAT(%s, '%s')" % (field_name, format_str) 
    138140        return sql 
    139141 
    140142    def drop_foreignkey_sql(self): 
  • db/backends/util.py

     
    5050############################################### 
    5151 
    5252def typecast_date(s): 
    53     return s and datetime.date(*map(int, s.split('-'))) or None # returns None if s is null 
     53    dt = None 
     54    try: # this will assure that if we receive dates like '0000-00-00 00:00:00' we do not crash and return None instead 
     55        dt = datetime.date(*map(int, s.split('-'))) 
     56    except: 
     57        dt = None 
     58    return dt # returns None if s is null or had another invalid value 
    5459 
    5560def typecast_time(s): # does NOT store time zone information 
    5661    if not s: return None 
     
    8489        seconds, microseconds = seconds.split('.') 
    8590    else: 
    8691        microseconds = '0' 
    87     return datetime.datetime(int(dates[0]), int(dates[1]), int(dates[2]), 
    88         int(times[0]), int(times[1]), int(seconds), int(float('.'+microseconds) * 1000000)) 
     92    result = None 
     93    try: # this will assure that if we receive dates like '0000-00-00 00:00:00' we do not crash and return None instead 
     94        result = datetime.datetime(int(dates[0]), int(dates[1]), int(dates[2]), 
     95            int(times[0]), int(times[1]), int(seconds), int(float('.'+microseconds) * 1000000)) 
     96    except: 
     97        result = None 
     98    return result 
    8999 
    90100def typecast_boolean(s): 
    91101    if s is None: return None