mirror of https://github.com/django/django.git
Fixed #28329 -- Fixed Cast() with Big/Small/Positive/PositiveSmallIntegerField on MySQL.
This commit is contained in:
parent
a96b981d84
commit
0f91ba1adc
|
@ -12,7 +12,11 @@ class Cast(Func):
|
||||||
mysql_types = {
|
mysql_types = {
|
||||||
fields.CharField: 'char',
|
fields.CharField: 'char',
|
||||||
fields.IntegerField: 'signed integer',
|
fields.IntegerField: 'signed integer',
|
||||||
|
fields.BigIntegerField: 'signed integer',
|
||||||
|
fields.SmallIntegerField: 'signed integer',
|
||||||
fields.FloatField: 'signed',
|
fields.FloatField: 'signed',
|
||||||
|
fields.PositiveIntegerField: 'unsigned integer',
|
||||||
|
fields.PositiveSmallIntegerField: 'unsigned integer',
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self, expression, output_field):
|
def __init__(self, expression, output_field):
|
||||||
|
|
|
@ -9,7 +9,7 @@ from .models import Author
|
||||||
class CastTests(TestCase):
|
class CastTests(TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpTestData(self):
|
def setUpTestData(self):
|
||||||
Author.objects.create(name='Bob', age=1)
|
Author.objects.create(name='Bob', age=1, alias='1')
|
||||||
|
|
||||||
def test_cast_from_value(self):
|
def test_cast_from_value(self):
|
||||||
numbers = Author.objects.annotate(cast_integer=Cast(Value('0'), models.IntegerField()))
|
numbers = Author.objects.annotate(cast_integer=Cast(Value('0'), models.IntegerField()))
|
||||||
|
@ -19,6 +19,18 @@ class CastTests(TestCase):
|
||||||
numbers = Author.objects.annotate(cast_string=Cast('age', models.CharField(max_length=255)),)
|
numbers = Author.objects.annotate(cast_string=Cast('age', models.CharField(max_length=255)),)
|
||||||
self.assertEqual(numbers.get().cast_string, '1')
|
self.assertEqual(numbers.get().cast_string, '1')
|
||||||
|
|
||||||
|
def test_cast_to_integer(self):
|
||||||
|
for field_class in (
|
||||||
|
models.IntegerField,
|
||||||
|
models.BigIntegerField,
|
||||||
|
models.SmallIntegerField,
|
||||||
|
models.PositiveIntegerField,
|
||||||
|
models.PositiveSmallIntegerField,
|
||||||
|
):
|
||||||
|
with self.subTest(field_class=field_class):
|
||||||
|
numbers = Author.objects.annotate(cast_int=Cast('alias', field_class()))
|
||||||
|
self.assertEqual(numbers.get().cast_int, 1)
|
||||||
|
|
||||||
def test_cast_from_python(self):
|
def test_cast_from_python(self):
|
||||||
numbers = Author.objects.annotate(cast_float=Cast(0, models.FloatField()))
|
numbers = Author.objects.annotate(cast_float=Cast(0, models.FloatField()))
|
||||||
self.assertEqual(numbers.get().cast_float, 0.0)
|
self.assertEqual(numbers.get().cast_float, 0.0)
|
||||||
|
|
Loading…
Reference in New Issue