Python django.db.models.functions.Upper() Examples

The following are 17 code examples of django.db.models.functions.Upper(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.db.models.functions , or try the search function .
Example #1
Source File: tests.py    From django-sqlserver with MIT License 6 votes vote down vote up
def test_order_by_nulls_last(self):
        self.skipTest("TODO fix django.db.utils.ProgrammingError: Incorrect syntax near 'NULLS'.")
        Article.objects.filter(headline="Article 3").update(author=self.author_1)
        Article.objects.filter(headline="Article 4").update(author=self.author_2)
        # asc and desc are chainable with nulls_last.
        self.assertSequenceEqual(
            Article.objects.order_by(F("author").desc(nulls_last=True)),
            [self.a4, self.a3, self.a1, self.a2],
        )
        self.assertSequenceEqual(
            Article.objects.order_by(F("author").asc(nulls_last=True)),
            [self.a3, self.a4, self.a1, self.a2],
        )
        self.assertSequenceEqual(
            Article.objects.order_by(Upper("author__name").desc(nulls_last=True)),
            [self.a4, self.a3, self.a1, self.a2],
        )
        self.assertSequenceEqual(
            Article.objects.order_by(Upper("author__name").asc(nulls_last=True)),
            [self.a3, self.a4, self.a1, self.a2],
        ) 
Example #2
Source File: tests.py    From django-sqlserver with MIT License 6 votes vote down vote up
def test_order_by_nulls_first(self):
        self.skipTest("TODO fix django.db.utils.ProgrammingError: Incorrect syntax near 'NULLS'.")
        Article.objects.filter(headline="Article 3").update(author=self.author_1)
        Article.objects.filter(headline="Article 4").update(author=self.author_2)
        # asc and desc are chainable with nulls_first.
        self.assertSequenceEqual(
            Article.objects.order_by(F("author").asc(nulls_first=True)),
            [self.a1, self.a2, self.a3, self.a4],
        )
        self.assertSequenceEqual(
            Article.objects.order_by(F("author").desc(nulls_first=True)),
            [self.a1, self.a2, self.a4, self.a3],
        )
        self.assertSequenceEqual(
            Article.objects.order_by(Upper("author__name").asc(nulls_first=True)),
            [self.a1, self.a2, self.a3, self.a4],
        )
        self.assertSequenceEqual(
            Article.objects.order_by(Upper("author__name").desc(nulls_first=True)),
            [self.a1, self.a2, self.a4, self.a3],
        ) 
Example #3
Source File: test_upper.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_basic(self):
        Author.objects.create(name='John Smith', alias='smithj')
        Author.objects.create(name='Rhonda')
        authors = Author.objects.annotate(upper_name=Upper('name'))
        self.assertQuerysetEqual(
            authors.order_by('name'), [
                'JOHN SMITH',
                'RHONDA',
            ],
            lambda a: a.upper_name
        )
        Author.objects.update(name=Upper('name'))
        self.assertQuerysetEqual(
            authors.order_by('name'), [
                ('JOHN SMITH', 'JOHN SMITH'),
                ('RHONDA', 'RHONDA'),
            ],
            lambda a: (a.upper_name, a.name)
        ) 
Example #4
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_order_by_nulls_last(self):
        Article.objects.filter(headline="Article 3").update(author=self.author_1)
        Article.objects.filter(headline="Article 4").update(author=self.author_2)
        # asc and desc are chainable with nulls_last.
        self.assertQuerysetEqualReversible(
            Article.objects.order_by(F("author").desc(nulls_last=True), 'headline'),
            [self.a4, self.a3, self.a1, self.a2],
        )
        self.assertQuerysetEqualReversible(
            Article.objects.order_by(F("author").asc(nulls_last=True), 'headline'),
            [self.a3, self.a4, self.a1, self.a2],
        )
        self.assertQuerysetEqualReversible(
            Article.objects.order_by(Upper("author__name").desc(nulls_last=True), 'headline'),
            [self.a4, self.a3, self.a1, self.a2],
        )
        self.assertQuerysetEqualReversible(
            Article.objects.order_by(Upper("author__name").asc(nulls_last=True), 'headline'),
            [self.a3, self.a4, self.a1, self.a2],
        ) 
Example #5
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_order_by_nulls_first(self):
        Article.objects.filter(headline="Article 3").update(author=self.author_1)
        Article.objects.filter(headline="Article 4").update(author=self.author_2)
        # asc and desc are chainable with nulls_first.
        self.assertQuerysetEqualReversible(
            Article.objects.order_by(F("author").asc(nulls_first=True), 'headline'),
            [self.a1, self.a2, self.a3, self.a4],
        )
        self.assertQuerysetEqualReversible(
            Article.objects.order_by(F("author").desc(nulls_first=True), 'headline'),
            [self.a1, self.a2, self.a4, self.a3],
        )
        self.assertQuerysetEqualReversible(
            Article.objects.order_by(Upper("author__name").asc(nulls_first=True), 'headline'),
            [self.a1, self.a2, self.a3, self.a4],
        )
        self.assertQuerysetEqualReversible(
            Article.objects.order_by(Upper("author__name").desc(nulls_first=True), 'headline'),
            [self.a1, self.a2, self.a4, self.a3],
        ) 
Example #6
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_order_by_nulls_first(self):
        Article.objects.filter(headline="Article 3").update(author=self.author_1)
        Article.objects.filter(headline="Article 4").update(author=self.author_2)
        # asc and desc are chainable with nulls_first.
        self.assertQuerysetEqualReversible(
            Article.objects.order_by(F("author").asc(nulls_first=True), 'headline'),
            [self.a1, self.a2, self.a3, self.a4],
        )
        self.assertQuerysetEqualReversible(
            Article.objects.order_by(F("author").desc(nulls_first=True), 'headline'),
            [self.a1, self.a2, self.a4, self.a3],
        )
        self.assertQuerysetEqualReversible(
            Article.objects.order_by(Upper("author__name").asc(nulls_first=True), 'headline'),
            [self.a1, self.a2, self.a3, self.a4],
        )
        self.assertQuerysetEqualReversible(
            Article.objects.order_by(Upper("author__name").desc(nulls_first=True), 'headline'),
            [self.a1, self.a2, self.a4, self.a3],
        ) 
Example #7
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_order_by_nulls_last(self):
        Article.objects.filter(headline="Article 3").update(author=self.author_1)
        Article.objects.filter(headline="Article 4").update(author=self.author_2)
        # asc and desc are chainable with nulls_last.
        self.assertQuerysetEqualReversible(
            Article.objects.order_by(F("author").desc(nulls_last=True), 'headline'),
            [self.a4, self.a3, self.a1, self.a2],
        )
        self.assertQuerysetEqualReversible(
            Article.objects.order_by(F("author").asc(nulls_last=True), 'headline'),
            [self.a3, self.a4, self.a1, self.a2],
        )
        self.assertQuerysetEqualReversible(
            Article.objects.order_by(Upper("author__name").desc(nulls_last=True), 'headline'),
            [self.a4, self.a3, self.a1, self.a2],
        )
        self.assertQuerysetEqualReversible(
            Article.objects.order_by(Upper("author__name").asc(nulls_last=True), 'headline'),
            [self.a3, self.a4, self.a1, self.a2],
        ) 
Example #8
Source File: test_substr.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_expressions(self):
        Author.objects.create(name='John Smith', alias='smithj')
        Author.objects.create(name='Rhonda')
        substr = Substr(Upper('name'), StrIndex('name', V('h')), 5, output_field=CharField())
        authors = Author.objects.annotate(name_part=substr)
        self.assertQuerysetEqual(
            authors.order_by('name'), ['HN SM', 'HONDA'],
            lambda a: a.name_part
        ) 
Example #9
Source File: test_functions.py    From django-mysql with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_if_with_string_values(self):
        Alphabet.objects.create(a=1, d="Lentils")
        Alphabet.objects.create(a=2, d="Cabbage")
        Alphabet.objects.create(a=3, d="Rice")

        result = list(
            Alphabet.objects.annotate(conditional=If(Q(a=2), Upper("d"), Lower("d")))
            .order_by("id")
            .values_list("conditional", flat=True)
        )
        assert result == ["lentils", "CABBAGE", "rice"] 
Example #10
Source File: test_upper.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_transform(self):
        with register_lookup(CharField, Upper):
            Author.objects.create(name='John Smith', alias='smithj')
            Author.objects.create(name='Rhonda')
            authors = Author.objects.filter(name__upper__exact='JOHN SMITH')
            self.assertQuerysetEqual(
                authors.order_by('name'), [
                    'John Smith',
                ],
                lambda a: a.name
            ) 
Example #11
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_function_as_filter(self):
        Author.objects.create(name='John Smith', alias='SMITHJ')
        Author.objects.create(name='Rhonda')
        self.assertQuerysetEqual(
            Author.objects.filter(alias=Upper(V('smithj'))),
            ['John Smith'], lambda x: x.name
        )
        self.assertQuerysetEqual(
            Author.objects.exclude(alias=Upper(V('smithj'))),
            ['Rhonda'], lambda x: x.name
        ) 
Example #12
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_specified_ordering_by_f_expression_without_asc_desc(self):
        class OrderedByFBandAdmin(admin.ModelAdmin):
            list_display = ['name', 'genres', 'nr_of_members']
            ordering = (F('nr_of_members'), Upper('name'), F('genres'))

        m = OrderedByFBandAdmin(Band, custom_site)
        request = self.factory.get('/band/')
        request.user = self.superuser
        cl = m.get_changelist_instance(request)
        self.assertEqual(cl.get_ordering_field_columns(), {3: 'asc', 2: 'asc'}) 
Example #13
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_specified_ordering_by_f_expression(self):
        class OrderedByFBandAdmin(admin.ModelAdmin):
            list_display = ['name', 'genres', 'nr_of_members']
            ordering = (
                F('nr_of_members').desc(nulls_last=True),
                Upper(F('name')).asc(),
                F('genres').asc(),
            )

        m = OrderedByFBandAdmin(Band, custom_site)
        request = self.factory.get('/band/')
        request.user = self.superuser
        cl = m.get_changelist_instance(request)
        self.assertEqual(cl.get_ordering_field_columns(), {3: 'desc', 2: 'asc'}) 
Example #14
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_valid_expression(self):
        class TestModelAdmin(ModelAdmin):
            ordering = (Upper('name'), Upper('band__name').desc())

        self.assertIsValid(TestModelAdmin, ValidationTestModel) 
Example #15
Source File: test_api.py    From django-perf-rec with MIT License 5 votes vote down vote up
def test_non_deterministic_QuerySet_annotate(self):
        with record():
            list(Author.objects.annotate(x=Upper("name"), y=Upper("name"))) 
Example #16
Source File: managers.py    From YaraGuardian with Apache License 2.0 5 votes vote down vote up
def change_name_case(self, operation, modifier=None):
        available_operations = {'lowercase': lowercase,
                                'uppercase': uppercase}

        edit = available_operations.get(operation, None)

        if edit and modifier:
            modification = edit(modifier)
            self.update(name=REPLACE('name', Value(modifier), Value(modification)))

        elif operation == 'lowercase':
            self.update(name=Lower('name'))

        elif operation == 'uppercase':
            self.update(name=Upper('name')) 
Example #17
Source File: test_functions.py    From django-mysql with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_if_field_lookups_work(self):
        Alphabet.objects.create(a=1, d="Lentils")
        Alphabet.objects.create(a=2, d="Cabbage")
        Alphabet.objects.create(a=3, d="Rice")

        result = list(
            Alphabet.objects.annotate(
                conditional=If(Q(a__gte=2), Upper("d"), Value(""))
            )
            .filter(conditional__startswith="C")
            .order_by("id")
            .values_list("conditional", flat=True)
        )
        assert result == ["CABBAGE"]