Пример #1
0
 def test_basic(self):
     Author.objects.create(name='John', alias='xyz')
     tests = (
         (Repeat('name', 0), ''),
         (Repeat('name', 2), 'JohnJohn'),
         (Repeat('name', Length('alias'), output_field=CharField()), 'JohnJohnJohn'),
         (Repeat(Value('x'), 3, output_field=CharField()), 'xxx'),
     )
     for function, repeated_text in tests:
         with self.subTest(function=function):
             authors = Author.objects.annotate(repeated_text=function)
             self.assertQuerysetEqual(authors, [repeated_text], lambda a: a.repeated_text, ordered=False)
 def test_filter_with_expr(self):
     # contains uses the UUID rather tan the ShortUUID
     self.assertSequenceEqual(
         NullableShortUUIDModel.objects.annotate(
             value=Concat(Value('8400'),
                          Value('e29b'),
                          output_field=CharField()), ).filter(
                              field__contains=F('value')),
         [self.objs[1]],
     )
     self.assertSequenceEqual(
         NullableShortUUIDModel.objects.annotate(value=Concat(
             Value('8400'),
             Value('-'),
             Value('e29b'),
             output_field=CharField()), ).filter(
                 field__contains=F('value')),
         [self.objs[1]],
     )
     self.assertSequenceEqual(
         NullableShortUUIDModel.objects.annotate(value=Repeat(
             Value('0'), 2, output_field=CharField()), ).filter(
                 field__contains=F('value')),
         [self.objs[1]],
     )
 def test_filter_with_expr(self):
     self.assertSequenceEqualWithoutHyphens(
         NullableUUIDModel.objects.annotate(
             value=Concat(Value('8400'), Value('e29b'), output_field=CharField()),
         ).filter(field__contains=F('value')),
         [self.objs[1]],
     )
     self.assertSequenceEqual(
         NullableUUIDModel.objects.annotate(
             value=Concat(Value('8400'), Value('-'), Value('e29b'), output_field=CharField()),
         ).filter(field__contains=F('value')),
         [self.objs[1]],
     )
     self.assertSequenceEqual(
         NullableUUIDModel.objects.annotate(
             value=Repeat(Value('0'), 4, output_field=CharField()),
         ).filter(field__contains=F('value')),
         [self.objs[1]],
     )
Пример #4
0
 def test_basic(self):
     Author.objects.create(name='John', alias='xyz')
     none_value = '' if connection.features.interprets_empty_strings_as_nulls else None
     tests = (
         (Repeat('name', 0), ''),
         (Repeat('name', 2), 'JohnJohn'),
         (Repeat('name', Length('alias')), 'JohnJohnJohn'),
         (Repeat(Value('x'), 3), 'xxx'),
         (Repeat('name', None), none_value),
         (Repeat(Value(None), 4), none_value),
         (Repeat('goes_by', 1), none_value),
     )
     for function, repeated_text in tests:
         with self.subTest(function=function):
             authors = Author.objects.annotate(repeated_text=function)
             self.assertQuerysetEqual(authors, [repeated_text],
                                      lambda a: a.repeated_text,
                                      ordered=False)
Пример #5
0
 def test_basic(self):
     Author.objects.create(name="John", alias="xyz")
     none_value = (""
                   if connection.features.interprets_empty_strings_as_nulls
                   else None)
     tests = (
         (Repeat("name", 0), ""),
         (Repeat("name", 2), "JohnJohn"),
         (Repeat("name", Length("alias")), "JohnJohnJohn"),
         (Repeat(Value("x"), 3), "xxx"),
         (Repeat("name", None), none_value),
         (Repeat(Value(None), 4), none_value),
         (Repeat("goes_by", 1), none_value),
     )
     for function, repeated_text in tests:
         with self.subTest(function=function):
             authors = Author.objects.annotate(repeated_text=function)
             self.assertQuerysetEqual(authors, [repeated_text],
                                      lambda a: a.repeated_text,
                                      ordered=False)
Пример #6
0
 def test_negative_number(self):
     with self.assertRaisesMessage(
             ValueError, "'number' must be greater or equal to 0."):
         Repeat("name", -1)
Пример #7
0
#!/usr/bin/env python
from django.db.models.functions import Repeat
from .models import MyModel
"""
https://docs.djangoproject.com/en/dev/ref/models/database-functions/#repeat

class Repeat(expression, number, **extra)
"""

qs = MyModel.objects.annotate(name_3x=Repeat('name', 3))
for r in qs.all():
    print(r.name_3x)