Django: how to go to class indexes

Django 1.11 introduced Class Based Model Indexes . What was previously defined as

class A(models.Model):
    class Meta:
        index_together = [
            ('foo', 'bar'),
        ]

      

Now can be defined as

class A(models.Model):
    class Meta:
        indexes = [
            models.Index(fields=['foo', 'bar']),
        ]

      

When I go to new syntax for model run python manage.py makemigrations

it will create migrations like

class Migration(migrations.Migration):

    dependencies = [
        ('app', '0001_initial'),
    ]

    operations = [
        migrations.AlterIndexTogether(
            name='a',
            index_together=set([]),
        ),
        migrations.AddIndex(
            model_name='a',
            index=models.Index(fields=['foo', 'bar'], name='app_a_f12345_idx'),
        ),
    ]

      

This migration will delete and recreate my indexes, which I would like to avoid.

What is the recommended way to go from old to new syntax? I couldn't find anything in the documentation.

+3


source to share


1 answer


You can edit the generated migration to name

match your original index index_together

, then run manage.py migrate

with the option --fake

.



+2


source







All Articles