summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Fedoseev <fedoseev.sergey@gmail.com>2017-03-29 22:29:53 +0500
committerTim Graham <timograham@gmail.com>2017-03-30 08:47:16 -0400
commit8484cf4cd0a6d3553f6fc1d62fa6dc04f04624f0 (patch)
tree54718e153fcaee37e2553fe1ae303a732fb44589
parentd476fa96ac1a91b5dbb9cf735665bb8755d1ee2a (diff)
[1.11.x] Refs #18247 -- Fixed SQLite QuerySet filtering on decimal result of Least and Greatest.
Backport of 068d75688f28f9a1530b771ae1d625f41e98cd82 from master
-rw-r--r--django/db/models/expressions.py4
-rw-r--r--django/db/models/functions/base.py4
-rw-r--r--tests/db_functions/models.py5
-rw-r--r--tests/db_functions/tests.py21
4 files changed, 29 insertions, 5 deletions
diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py
index a05a1b35ab..b1d67e6653 100644
--- a/django/db/models/expressions.py
+++ b/django/db/models/expressions.py
@@ -570,8 +570,8 @@ class Func(Expression):
data['expressions'] = data['field'] = arg_joiner.join(sql_parts)
return template % data, params
- def as_sqlite(self, compiler, connection):
- sql, params = self.as_sql(compiler, connection)
+ def as_sqlite(self, compiler, connection, **extra_context):
+ sql, params = self.as_sql(compiler, connection, **extra_context)
try:
if self.output_field.get_internal_type() == 'DecimalField':
sql = 'CAST(%s AS NUMERIC)' % sql
diff --git a/django/db/models/functions/base.py b/django/db/models/functions/base.py
index ca73340b85..8adba2743c 100644
--- a/django/db/models/functions/base.py
+++ b/django/db/models/functions/base.py
@@ -137,7 +137,7 @@ class Greatest(Func):
def as_sqlite(self, compiler, connection):
"""Use the MAX function on SQLite."""
- return super(Greatest, self).as_sql(compiler, connection, function='MAX')
+ return super(Greatest, self).as_sqlite(compiler, connection, function='MAX')
class Least(Func):
@@ -157,7 +157,7 @@ class Least(Func):
def as_sqlite(self, compiler, connection):
"""Use the MIN function on SQLite."""
- return super(Least, self).as_sql(compiler, connection, function='MIN')
+ return super(Least, self).as_sqlite(compiler, connection, function='MIN')
class Length(Transform):
diff --git a/tests/db_functions/models.py b/tests/db_functions/models.py
index 7494680c23..63359b7948 100644
--- a/tests/db_functions/models.py
+++ b/tests/db_functions/models.py
@@ -56,3 +56,8 @@ class DTModel(models.Model):
def __str__(self):
return 'DTModel({0})'.format(self.name)
+
+
+class DecimalModel(models.Model):
+ n1 = models.DecimalField(decimal_places=2, max_digits=6)
+ n2 = models.DecimalField(decimal_places=2, max_digits=6)
diff --git a/tests/db_functions/tests.py b/tests/db_functions/tests.py
index 26acc687ea..3041f29570 100644
--- a/tests/db_functions/tests.py
+++ b/tests/db_functions/tests.py
@@ -1,6 +1,7 @@
from __future__ import unicode_literals
from datetime import datetime, timedelta
+from decimal import Decimal
from unittest import skipIf, skipUnless
from django.db import connection
@@ -13,7 +14,7 @@ from django.db.models.functions import (
from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
from django.utils import timezone
-from .models import Article, Author, Fan
+from .models import Article, Author, DecimalModel, Fan
lorem_ipsum = """
@@ -204,6 +205,15 @@ class FunctionTests(TestCase):
author.refresh_from_db()
self.assertEqual(author.alias, 'Jim')
+ def test_greatest_decimal_filter(self):
+ obj = DecimalModel.objects.create(n1=Decimal('1.1'), n2=Decimal('1.2'))
+ self.assertCountEqual(
+ DecimalModel.objects.annotate(
+ greatest=Greatest('n1', 'n2'),
+ ).filter(greatest=Decimal('1.2')),
+ [obj],
+ )
+
def test_least(self):
now = timezone.now()
before = now - timedelta(hours=1)
@@ -299,6 +309,15 @@ class FunctionTests(TestCase):
author.refresh_from_db()
self.assertEqual(author.alias, 'James Smith')
+ def test_least_decimal_filter(self):
+ obj = DecimalModel.objects.create(n1=Decimal('1.1'), n2=Decimal('1.2'))
+ self.assertCountEqual(
+ DecimalModel.objects.annotate(
+ least=Least('n1', 'n2'),
+ ).filter(least=Decimal('1.1')),
+ [obj],
+ )
+
def test_concat(self):
Author.objects.create(name='Jayden')
Author.objects.create(name='John Smith', alias='smithj', goes_by='John')