13👍
According to the docs, you can give indexes a name and you can set an order for each field in the index (but not all DBs are equally supported).
But in reality the whole point of this is to bring extra flexibility to define other types of indexes where supported.
Currently as of Django 1.11 and only when using Postgres you can define GIN and BRIN indexes if that is something that would fit your needs. (see django.contrib.postgres.indexes at https://docs.djangoproject.com/en/1.11/ref/contrib/postgres/indexes/)
Being able to define indexes as you described is just the most general case and unless you want to use a feature named above (naming indexes or ordering fields) you can probably use indexes as you’ve been doing until now (db_index and index_together).
0👍
In Django, the
db_index=True
option is used in model field and
indexes
used in meta. Their concept is same. They creates an index
(B-Tree) in the database.. You may either usedb_index_True
or
metaindexes
Yes, The code
- example-1 and example-2 doing the same thing
# example-1
class Person(models.Model):
name = models.CharField(db_index=True)
age = models.IntegerField(db_index=True)
# example-2
class Person(models.Model):
name = models.CharField()
age = models.IntegerField()
class Meta:
indexes = [
models.Index(fields=['name']),
models.Index(fields=['age'])
]
- example-3 and example-4 are same as well
# example 3
class Person(models.Model):
name = models.CharField()
age = models.IntegerField()
class Meta:
indexes = [
models.Index(fields=['name', 'age'])
]
# example 4
class Person(models.Model):
name = models.CharField()
age = models.IntegerField()
class Meta:
index_together = [['name', 'age']]
- [Django]-Django F expressions joined field
- [Django]-How to check if an element is present in a Django queryset?
- [Django]-What is the best location to put templates in django project?