Skip to content

Commit 8484cf4

Browse files
sir-sigurdtimgraham
authored andcommitted
[1.11.x] Refs django#18247 -- Fixed SQLite QuerySet filtering on decimal result of Least and Greatest.
Backport of 068d756 from master
1 parent d476fa9 commit 8484cf4

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

django/db/models/expressions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,8 +570,8 @@ def as_sql(self, compiler, connection, function=None, template=None, arg_joiner=
570570
data['expressions'] = data['field'] = arg_joiner.join(sql_parts)
571571
return template % data, params
572572

573-
def as_sqlite(self, compiler, connection):
574-
sql, params = self.as_sql(compiler, connection)
573+
def as_sqlite(self, compiler, connection, **extra_context):
574+
sql, params = self.as_sql(compiler, connection, **extra_context)
575575
try:
576576
if self.output_field.get_internal_type() == 'DecimalField':
577577
sql = 'CAST(%s AS NUMERIC)' % sql

django/db/models/functions/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def __init__(self, *expressions, **extra):
137137

138138
def as_sqlite(self, compiler, connection):
139139
"""Use the MAX function on SQLite."""
140-
return super(Greatest, self).as_sql(compiler, connection, function='MAX')
140+
return super(Greatest, self).as_sqlite(compiler, connection, function='MAX')
141141

142142

143143
class Least(Func):
@@ -157,7 +157,7 @@ def __init__(self, *expressions, **extra):
157157

158158
def as_sqlite(self, compiler, connection):
159159
"""Use the MIN function on SQLite."""
160-
return super(Least, self).as_sql(compiler, connection, function='MIN')
160+
return super(Least, self).as_sqlite(compiler, connection, function='MIN')
161161

162162

163163
class Length(Transform):

tests/db_functions/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,8 @@ class DTModel(models.Model):
5656

5757
def __str__(self):
5858
return 'DTModel({0})'.format(self.name)
59+
60+
61+
class DecimalModel(models.Model):
62+
n1 = models.DecimalField(decimal_places=2, max_digits=6)
63+
n2 = models.DecimalField(decimal_places=2, max_digits=6)

tests/db_functions/tests.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import unicode_literals
22

33
from datetime import datetime, timedelta
4+
from decimal import Decimal
45
from unittest import skipIf, skipUnless
56

67
from django.db import connection
@@ -13,7 +14,7 @@
1314
from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
1415
from django.utils import timezone
1516

16-
from .models import Article, Author, Fan
17+
from .models import Article, Author, DecimalModel, Fan
1718

1819

1920
lorem_ipsum = """
@@ -204,6 +205,15 @@ def test_greatest_update(self):
204205
author.refresh_from_db()
205206
self.assertEqual(author.alias, 'Jim')
206207

208+
def test_greatest_decimal_filter(self):
209+
obj = DecimalModel.objects.create(n1=Decimal('1.1'), n2=Decimal('1.2'))
210+
self.assertCountEqual(
211+
DecimalModel.objects.annotate(
212+
greatest=Greatest('n1', 'n2'),
213+
).filter(greatest=Decimal('1.2')),
214+
[obj],
215+
)
216+
207217
def test_least(self):
208218
now = timezone.now()
209219
before = now - timedelta(hours=1)
@@ -299,6 +309,15 @@ def test_least_update(self):
299309
author.refresh_from_db()
300310
self.assertEqual(author.alias, 'James Smith')
301311

312+
def test_least_decimal_filter(self):
313+
obj = DecimalModel.objects.create(n1=Decimal('1.1'), n2=Decimal('1.2'))
314+
self.assertCountEqual(
315+
DecimalModel.objects.annotate(
316+
least=Least('n1', 'n2'),
317+
).filter(least=Decimal('1.1')),
318+
[obj],
319+
)
320+
302321
def test_concat(self):
303322
Author.objects.create(name='Jayden')
304323
Author.objects.create(name='John Smith', alias='smithj', goes_by='John')

0 commit comments

Comments
 (0)