summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Smeaton <josh.smeaton@gmail.com>2015-01-20 11:47:13 +1100
committerJosh Smeaton <josh.smeaton@gmail.com>2015-01-20 12:11:27 +1100
commit504cd5d3be450376f462b66e36df6edc923e9496 (patch)
tree65acf5db4b5caf48ced9c93c37350850d09105cc
parente69eea73d94c058728143416058a6257c765a9b8 (diff)
[1.8.x] Fixed #24183 -- Fixed wrong comparisons in Substr
Backport of 61c102d010ef480cebe576cc1576d1101975925c from master
-rw-r--r--django/db/models/functions.py4
-rw-r--r--tests/db_functions/tests.py12
2 files changed, 14 insertions, 2 deletions
diff --git a/django/db/models/functions.py b/django/db/models/functions.py
index f16cf10b15..610ecb6985 100644
--- a/django/db/models/functions.py
+++ b/django/db/models/functions.py
@@ -110,13 +110,13 @@ class Substr(Func):
pos: an integer > 0, or an expression returning an integer
length: an optional number of characters to return
"""
- if not hasattr('pos', 'resolve_expression'):
+ if not hasattr(pos, 'resolve_expression'):
if pos < 1:
raise ValueError("'pos' must be greater than 0")
pos = Value(pos)
expressions = [expression, pos]
if length is not None:
- if not hasattr('length', 'resolve_expression'):
+ if not hasattr(length, 'resolve_expression'):
length = Value(length)
expressions.append(length)
super(Substr, self).__init__(*expressions, **extra)
diff --git a/tests/db_functions/tests.py b/tests/db_functions/tests.py
index 00a5e258de..97d32d86e7 100644
--- a/tests/db_functions/tests.py
+++ b/tests/db_functions/tests.py
@@ -278,6 +278,18 @@ class FunctionTests(TestCase):
with six.assertRaisesRegex(self, ValueError, "'pos' must be greater than 0"):
Author.objects.annotate(raises=Substr('name', 0))
+ def test_substr_with_expressions(self):
+ Author.objects.create(name='John Smith', alias='smithj')
+ Author.objects.create(name='Rhonda')
+ authors = Author.objects.annotate(name_part=Substr('name', V(5), V(3)))
+ self.assertQuerysetEqual(
+ authors.order_by('name'), [
+ ' Sm',
+ 'da',
+ ],
+ lambda a: a.name_part
+ )
+
def test_nested_function_ordering(self):
Author.objects.create(name='John Smith')
Author.objects.create(name='Rhonda Simpson', alias='ronny')