Adding indexes to model fields in Django with migrations

前端 未结 3 1779
灰色年华
灰色年华 2021-02-13 03:11

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

3条回答
  •  甜味超标
    2021-02-13 03:58

    This problem still exists in django2.1. I solved it by using the indexes Meta option. This is a bit cleaner than the index_together solution.

    class Person(models.Model):
        first_name = models.CharField()
        last_name = models.CharField()
    
        class Meta:
            indexes = [
                models.Index(fields=['last_name']),
            ]
    

提交回复
热议问题