text: Opened 5 years ago
  Closed 4 years ago
  Last modified 4 years ago
  #20587 closed Bug (fixed)
  sqlite3 backend fails to properly handle null dates in certain cases.
  Description
  When converting from the model field to a django type db/sqlite3/base.py uses a method called convert_values to coerce certain types to match what is returned by other backends. However, in certain cases this fails. One such case is when the field is a DateTimeField and the value is null.
  In that particular case, Django attempts to use parse_date() to coerce the value into a datetime value, which fails with a TypeError (since it is passed a null.)
  I believe the proper behaviour here is to simply short-circuit the logic and return None if the value in the field is None, if it is not None, assume it fits the specified type and allow the method to proceed as normal.
  So basically, this method definition:
  def convert_values(self, value, field): """SQLite returns floats when it should be returning decimals, and gets dates and datetimes wrong. For consistency with other backends, coerce when required. """ if value is None: return None internal_type = field.get_internal_type() if internal_type == 'DecimalField': return util.typecast_decimal(field.format_number(value)) elif internal_type and internal_type.endswith('IntegerField') or internal_type == 'AutoField': return int(value) elif internal_type == 'DateField': return parse_date(value) elif internal_type == 'DateTimeField': return parse_datetime_with_timezone_support(value) elif internal_type == 'TimeField': return parse_time(value) # No field, or the field isn't known to be a decimal or integer return value
  Change History (4)
  comment:1 Changed 5 years ago by
  comment:2 Changed 4 years ago by
  Can't mark as accepted as there is no test case. All that is needed is a model definition + a little code to show a case where the error happens.
  Can you write a test where this would occur?