I am trying to add indexes on model fields using Field.db_index
for an app that has migrations. Looking at Django\'s documentation all I need to do is to set
You can do this explicitly in your migration using Django's AddIndex and Index classes.
First create an empty migration with manage.py makemigrations --empty
and then simply fill it out as follows:
from django.db import migrations
from django.db.models.indexes import Index
from django.db.migrations import AddIndex
class Migration(migrations.Migration):
dependencies = [
('app_name', 'ref_to_previous_migration'),
]
operations = [
AddIndex('ModelName', Index(fields=['field_name'], name='my_index'))
]
You can use options on the Index
class to specify fields, add a name, and do special custom things like index only part of the table, etc. Check the doc links above.