Create a Django model index with an upper case empty string condition

Let’s say I have the following Django model. from django.db import models class User(models.Model): name = models.CharField(blank=True) And I want to filter those like so. users = User.objects.filter(name__istartswith="John") That would perform the following query. SELECT "data_user"."id", "data_user"."name" FROM "data_user" WHERE UPPER("data_user"."name"::text) LIKE UPPER('John%') I can improve the performance of that query by adding the following index to the model. To complement the use of UPPER() in the query I can use the Upper() function for the index’s expression....

May 2, 2024 · James Beith