sqlite3 backend fails to properly handle null dates in certain cases.
|Reported by:||c@…||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||1.5|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||yes||Patch needs improvement:||no|
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 3 years ago by Kamu
- Needs documentation unset
- Needs tests set
- Patch needs improvement unset
comment:3 Changed 3 years ago by Gilberto Gonçalves <lursty@…>
- Resolution set to fixed
- Status changed from new to closed