diff --git a/django/db/models/functions/text.py b/django/db/models/functions/text.py index 8715e86898..34421eb15d 100644 --- a/django/db/models/functions/text.py +++ b/django/db/models/functions/text.py @@ -42,6 +42,7 @@ class PostgreSQLSHAMixin: class Chr(Transform): function = "CHR" lookup_name = "chr" + output_field = CharField() def as_mysql(self, compiler, connection, **extra_context): return super().as_sql( diff --git a/tests/db_functions/text/test_chr.py b/tests/db_functions/text/test_chr.py index 7a55994096..fe1aea3f2c 100644 --- a/tests/db_functions/text/test_chr.py +++ b/tests/db_functions/text/test_chr.py @@ -1,4 +1,4 @@ -from django.db.models import IntegerField +from django.db.models import F, IntegerField from django.db.models.functions import Chr, Left, Ord from django.test import TestCase from django.test.utils import register_lookup @@ -37,3 +37,13 @@ class ChrTests(TestCase): authors.exclude(name_code_point__chr=Chr(ord("J"))), [self.elena, self.rhonda], ) + + def test_annotate(self): + authors = Author.objects.annotate( + first_initial=Left("name", 1), + initial_chr=Chr(ord("J")), + ) + self.assertSequenceEqual( + authors.filter(first_initial=F("initial_chr")), + [self.john], + )