mirror of https://github.com/django/django.git
Fixed #24183 -- Fixed wrong comparisons in Substr
This commit is contained in:
parent
7f20041bca
commit
61c102d010
|
@ -110,13 +110,13 @@ class Substr(Func):
|
||||||
pos: an integer > 0, or an expression returning an integer
|
pos: an integer > 0, or an expression returning an integer
|
||||||
length: an optional number of characters to return
|
length: an optional number of characters to return
|
||||||
"""
|
"""
|
||||||
if not hasattr('pos', 'resolve_expression'):
|
if not hasattr(pos, 'resolve_expression'):
|
||||||
if pos < 1:
|
if pos < 1:
|
||||||
raise ValueError("'pos' must be greater than 0")
|
raise ValueError("'pos' must be greater than 0")
|
||||||
pos = Value(pos)
|
pos = Value(pos)
|
||||||
expressions = [expression, pos]
|
expressions = [expression, pos]
|
||||||
if length is not None:
|
if length is not None:
|
||||||
if not hasattr('length', 'resolve_expression'):
|
if not hasattr(length, 'resolve_expression'):
|
||||||
length = Value(length)
|
length = Value(length)
|
||||||
expressions.append(length)
|
expressions.append(length)
|
||||||
super(Substr, self).__init__(*expressions, **extra)
|
super(Substr, self).__init__(*expressions, **extra)
|
||||||
|
|
|
@ -278,6 +278,18 @@ class FunctionTests(TestCase):
|
||||||
with six.assertRaisesRegex(self, ValueError, "'pos' must be greater than 0"):
|
with six.assertRaisesRegex(self, ValueError, "'pos' must be greater than 0"):
|
||||||
Author.objects.annotate(raises=Substr('name', 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):
|
def test_nested_function_ordering(self):
|
||||||
Author.objects.create(name='John Smith')
|
Author.objects.create(name='John Smith')
|
||||||
Author.objects.create(name='Rhonda Simpson', alias='ronny')
|
Author.objects.create(name='Rhonda Simpson', alias='ronny')
|
||||||
|
|
Loading…
Reference in New Issue