如 https://docs.djangoproject.com/en/1.10/ref/models/fields/#django.db.models.Field.db_index 中所述,您可以使用 db_index=True
将索引添加到表中。但是你如何强制哈希索引与 btree 索引?
最佳答案
自从提出问题以来,情况发生了很大变化。现在可以通过模型的元类在模型层中定义索引(至少从 django 1.11 开始): https://docs.djangoproject.com/en/dev/ref/contrib/postgres/indexes/
例如:
from django.contrib.postgres.indexes import HashIndex
from django.db import models
class User(models.Model):
name = models.CharField(max_length=100)
class Meta:
indexes = (HashIndex(fields=('name',)),)
关于 python Django : how to select the index type?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41955346/