我是否需要在此Django模型中添加db_index?

时间:2011-01-29 10:25:22

标签: python mysql database django indexing

class Comments(models.Model):
    content = models.ForeignKey(Content)

我是否需要在“内容”中添加db_index?或者是否会自动编入索引,因为它是外键?

1 个答案:

答案 0 :(得分:43)

除非另有说明,否则将为ForeignKey创建索引。相关源代码:

class ForeignKey(RelatedField, Field):
    # snip
    def __init__(self, to, to_field=None, rel_class=ManyToOneRel, **kwargs):
        # snip
        if 'db_index' not in kwargs:
            kwargs['db_index'] = True