﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
28727	Cast crashes on SQLite when casting a Python date/datetime to Date/DateTimeField	direx	Mariusz Felisiak	"When trying to cast a date object to a `DateField` in `annotate` an exception is raised:

{{{
import datetime
from django.contrib.auth.models import User
from django.db import models
from django.db.models.functions import Cast

User.objects.get_or_create(username=""test"")
User.objects.all().annotate(today=Cast(datetime.date.today(), models.DateField()))

Traceback (most recent call last):
  File ""<console>"", line 1, in <module>
  File ""/tmp/testenv/lib/python2.7/site-packages/django/db/models/query.py"", line 226, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File ""/tmp/testenv/lib/python2.7/site-packages/django/db/models/query.py"", line 250, in __iter__
    self._fetch_all()
  File ""/tmp/testenv/lib/python2.7/site-packages/django/db/models/query.py"", line 1118, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File ""/tmp/testenv/lib/python2.7/site-packages/django/db/models/query.py"", line 62, in __iter__
    for row in compiler.results_iter(results):
  File ""/tmp/testenv/lib/python2.7/site-packages/django/db/models/sql/compiler.py"", line 842, in results_iter
    row = self.apply_converters(row, converters)
  File ""/tmp/testenv/lib/python2.7/site-packages/django/db/models/sql/compiler.py"", line 827, in apply_converters
    value = converter(value, expression, self.connection, self.query.context)
  File ""/tmp/testenv/lib/python2.7/site-packages/django/db/backends/sqlite3/operations.py"", line 227, in convert_datefield_value
    value = parse_date(value)
  File ""/tmp/testenv/lib/python2.7/site-packages/django/utils/dateparse.py"", line 61, in parse_date
    match = date_re.match(value)
TypeError: expected string or buffer

}}}

This only seems to happen with SQLite, MySQL works as expected.

FYI, this is the SQL which is generated for SQLite:

{{{
SELECT ""auth_user"".""id"", ""auth_user"".""password"", ""auth_user"".""last_login"", ""auth_user"".""is_superuser"", ""auth_user"".""username"", ""auth_user"".""first_name"", ""auth_user"".""last_name"", ""auth_user"".""email"", ""auth_user"".""is_staff"", ""auth_user"".""is_active"", ""auth_user"".""date_joined"", CAST(2017-10-20 AS date) AS ""today"" FROM ""auth_user""
}}}
"	Bug	closed	Database layer (models, ORM)	dev	Normal	fixed	SQLite Cast	Simon Charette	Ready for checkin	1	0	0	0	0	0
