24
class Comments(models.Model):
content = models.ForeignKey(Content)
是否需要将db_index添加到“content”?或者会自动被索引,因为它是一个外键?我是否需要将db_index添加到此Django模型?
class Comments(models.Model):
content = models.ForeignKey(Content)
是否需要将db_index添加到“content”?或者会自动被索引,因为它是一个外键?我是否需要将db_index添加到此Django模型?
除非另有规定,否则将为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
要禁用在ForeignKey上创建索引,请设置`db_index = False`:http://code.djangoproject.com/ticket/13730 – 2014-02-03 14:10:18