3 ms·
The new index definitions look nice: from django.db import models class Customer(models.Model): first_name = models.CharField(max_length=100)
by rattray 10y ago
The new index definitions look nice:
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'),
]
Also supports ordering (eg; `'-first_name'` for DESC) and `BrinIndex`/`GinIndex`.
- ddorian43 10y agoDamn, those are new ? Or did just the formating change?
- chc 10y agoYou previously couldn't define the name or type of an index. You just listed tuples of fields to be indexed.
- Alex3917 10y agoThe old index syntax (e.g. unique=True) will still work, and is now considered a shortcut for the more verbose syntax. The idea is to have one standard syntax that supports creating non-btree indexes in the model definitions, rather than needing to create them via custom migrations.
- scrollaway 10y agoClass-based db indexes are new.
- ubernostrum 10y agoBit of both. Previously, you could indicate you wanted an index on a field, but only on a single field, and you did it in the field definition. Declaring them in Meta via the Index class is new, as is the multi-field support and direction support. More importantly, you can subclass Index to define other types of indices besides the default (which is btree). For example, Django now ships built-in subclasses to do BRIN and GIN on Postgres.
- deckiedan 10y agoIs there anywhere django docs on how to actually use the brin/gin indexes? All I can find is that they exist...
- rattray 10y agohttps://docs.djangoproject.com/en/1.11/ref/contrib/postgres/indexes/#module-django.contrib.postgres.indexes https://docs.djangoproject.com/en/1.11/ref/contrib/postgres/...
- minusf 10y agosingle field? what about `unique_together` and `index_together`?
- lsh 10y agohm. 'indexes' will bother me. I've lived with 'color' vs 'colour' for a long time, perhaps I'll be ok.