Beispiel #1
0
 def test_transform(self):
     with register_lookup(IntegerField, Chr):
         authors = Author.objects.annotate(name_code_point=Ord('name'))
         self.assertCountEqual(
             authors.filter(name_code_point__chr=Chr(ord('J'))),
             [self.john])
         self.assertCountEqual(
             authors.exclude(name_code_point__chr=Chr(ord('J'))),
             [self.elena, self.rhonda])
Beispiel #2
0
    def test_chr(self):
        Author.objects.create(name='John Smith', alias='smithj')
        Author.objects.create(name='Élena Jordan', alias='elena')
        Author.objects.create(name='Rhonda')
        authors = Author.objects.annotate(first_initial=Left('name', 1))

        self.assertQuerysetEqual(authors.filter(first_initial=Chr(ord('J'))),
                                 ['John Smith'], lambda x: x.name)
        self.assertQuerysetEqual(
            authors.exclude(first_initial=Chr(ord('J'))),
            ['Élena Jordan', 'Rhonda'],
            lambda x: x.name,
            ordered=False,
        )
Beispiel #3
0
 def test_chr_transform(self):
     try:
         IntegerField.register_lookup(Chr, 'chr')
         Author.objects.create(name='John Smith', alias='smithj')
         Author.objects.create(name='Élena Jordan', alias='elena')
         Author.objects.create(name='Rhonda')
         authors = Author.objects.annotate(name_code_point=Ord('name'))
         self.assertQuerysetEqual(
             authors.filter(name_code_point__chr=Chr(ord('J'))),
             ['John Smith'], lambda x: x.name)
         self.assertQuerysetEqual(
             authors.exclude(name_code_point__chr=Chr(ord('J'))),
             ['Élena Jordan', 'Rhonda'],
             lambda x: x.name,
             ordered=False,
         )
     finally:
         IntegerField._unregister_lookup(Chr, 'chr')
Beispiel #4
0
 def test_non_ascii(self):
     authors = Author.objects.annotate(first_initial=Left("name", 1))
     self.assertCountEqual(authors.filter(first_initial=Chr(ord("É"))),
                           [self.elena])
     self.assertCountEqual(authors.exclude(first_initial=Chr(ord("É"))),
                           [self.john, self.rhonda])
Beispiel #5
0
 def test_basic(self):
     authors = Author.objects.annotate(first_initial=Left('name', 1))
     self.assertCountEqual(authors.filter(first_initial=Chr(ord('J'))),
                           [self.john])
     self.assertCountEqual(authors.exclude(first_initial=Chr(ord('J'))),
                           [self.elena, self.rhonda])
Beispiel #6
0
#!/usr/bin/env python
from django.db.models.functions import Chr
from .models import MyModel
"""
https://docs.djangoproject.com/en/dev/ref/models/database-functions/#chr

class Chr(expression, **extra)
"""

qs = MyModel.objects.filter(name__startswith=Chr(ord('M')))
for r in qs.all():
    print(r.name)