32
As of Django 1.5, you can use the Meta.index_together
option:
class QuoteModel(models.Model):
# ... fields ...
class Meta:
index_together = [
("sequence", "stock"),
]
(note: the original answer from 2009 said it was not possible to index multiple fields; it has since been replaced)
24
There is a ticket for this feature. Take a look http://code.djangoproject.com/ticket/5805
You may apply the patch from this ticket yourself.
UPDATE
It’s now in Django: https://docs.djangoproject.com/en/1.5/ref/models/options/#django.db.models.Options.index_together
- [Django]-Count number of records by date in Django
- [Django]-OrderingFilter has no attribute 'filter_queryset'
- [Django]-403 Forbidden error when making an ajax Post request in Django framework
17
To follow on from the accepted answer, if you are using South, you can easily add a composite key as follows:
manage.py schemamigration your_app_name name_for_migration –add-index ModelName.first_field_in_index
You can then edit the generated migration file to add the additional fields into the one index (you’ll see it’s just a list of field names that’s needed).
Don’t forget to update the reverse migration as well as the forward one.
- [Django]-Custom error message in Django admin actions
- [Django]-Django Model Fields Indexing
- [Django]-Django 1.7 migrate gets error "table already exists"
16
It is index_together in django 1.5
See here https://docs.djangoproject.com/en/dev/ref/models/options/#index-together
- [Django]-How to use Python type hints with Django QuerySet?
- [Django]-How to debug Django commands in PyCharm
- [Django]-How to understand lazy function in Django utils functional module
11
unique_together
might be what you are looking for. Just put it in your Meta
class inside your model.
- [Django]-Django update queryset with annotation
- [Django]-How to get GET request values in Django?
- [Django]-Override default queryset in Django admin
11
The index_together feature could be deprecated in the futur.
You should use indexes option instead of index_together.
Example of indexes option :
from django.db import models
class Customer(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
class Meta:
indexes = [
models.Index(fields=['last_name', 'first_name']),
models.Index(fields=['first_name'], name='first_name_idx'),
]
- [Django]-Django: How to format a DateField's date representation?
- [Django]-Related_name argument not working as expected in Django model?
- [Django]-Django – is not a registered namespace
4
Update for 2022:
The newer indexes
option provides more functionality than index_together
. index_together
was deprecated in Django 4.1.
from django.db import models
class Customer(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
class Meta:
indexes = [
models.Index(fields=['last_name', 'first_name']),
models.Index(fields=['first_name'], name='first_name_idx'),
]
- [Django]-Django REST framework serializer without a model
- [Django]-How to get the value of a Django Model Field object
- [Django]-Django REST Framework (DRF): Set current user id as field value
2
I just wanted to add that as of Django 1.11 there is a new feature, Options.indexes, which will allow you to specify the indexes to create:
- [Django]-Django Setup Default Logging
- [Django]-Temporarily disable auto_now / auto_now_add
- [Django]-What is the equivalent of "none" in django templates?