[Answered ]-Indexing first n characters of Charfield in Django

1👍

You can create a functional index with the Substr function [Django-doc]:

from django.db.models.functions import Substr

# …

class Meta:
       indexes = [
           models.Index(fields=['last_name', 'first_name']),
           models.Index(fields=['-date_of_birth']),
           models.Index(Substr('pub_date', 0, 10), name='part_of_name')
       ]

Leave a comment