Fixed #28727 -- Fixed Cast crash on SQLite when casting a Python date/datetime to Date/DateTimeField.

This commit is contained in:
Mariusz Felisiak 2017-12-19 19:54:58 +01:00 committed by GitHub
parent 78247b80a8
commit 4420761ea9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View File

@ -14,6 +14,10 @@ from django.utils.duration import duration_string
class DatabaseOperations(BaseDatabaseOperations):
cast_char_field_without_max_length = 'text'
cast_data_types = {
'DateField': 'TEXT',
'DateTimeField': 'TEXT',
}
def bulk_batch_size(self, fields, objs):
"""

View File

@ -1,4 +1,6 @@
from django.db import models
import datetime
from django.db import connection, models
from django.db.models.expressions import Value
from django.db.models.functions import Cast
from django.test import TestCase, ignore_warnings, skipUnlessDBFeature
@ -42,6 +44,19 @@ class CastTests(TestCase):
numbers = Author.objects.annotate(cast_int=Cast('alias', field_class()))
self.assertEqual(numbers.get().cast_int, 1)
def test_cast_from_python_to_date(self):
today = datetime.date.today()
dates = Author.objects.annotate(cast_date=Cast(today, models.DateField()))
self.assertEqual(dates.get().cast_date, today)
def test_cast_from_python_to_datetime(self):
now = datetime.datetime.now()
if connection.vendor == 'oracle':
# Workaround until #28934 is fixed.
now = now.replace(microsecond=0)
dates = Author.objects.annotate(cast_datetime=Cast(now, models.DateTimeField()))
self.assertEqual(dates.get().cast_datetime, now)
def test_cast_from_python(self):
numbers = Author.objects.annotate(cast_float=Cast(0, models.FloatField()))
self.assertEqual(numbers.get().cast_float, 0.0)