mirror of
https://github.com/django/django.git
synced 2025-07-24 13:44:32 +00:00
Fixed #28727 -- Fixed Cast crash on SQLite when casting a Python date/datetime to Date/DateTimeField.
This commit is contained in:
parent
78247b80a8
commit
4420761ea9
2 changed files with 20 additions and 1 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue