from django.db.models import Transform
from django.db.models import CharField


class CharacterLength(Transform):
    lookup_name = 'len'

def as_sql(self, compiler, connection):
    lhs, params = compiler.compile(self.lhs)
return 'LENGTH(%s)' % lhs, params
CharField.register_lookup(CharacterLength)
Django - фильтровать набор запросов по длине значения CharField