FeroxTL / django-migration-control

Django migration control allows avoid unnecessary migrations such as changing help_text or verbose_name of fields

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

django-migration-control

Django migration control allows avoid unnecessary migrations such as changing help_text or verbose_name of fields

Installation

Warning! Beta version! Use it at your own risk

pip install django-migration-control

Requirements

Django>=1.7  # because we use django migrations
python>=2.7

Settings

EXCLUDE_FIELDS

default

EXCLUDE_FIELDS = ['help_text', 'verbose_name', 'choices']

Why?

Let's see an example. Suppose you have the model TestModel:

from django.db import models

class TestModel(models.Model):
    name = models.CharField(
        max_length=50,
        verbose_name='Name',
        help_text='This is demo help')

You already have made all migrations with ./manage.py makemigrations and your models are synced with database:

./manage.py migrate --list
demo
 [X] 0001_initial

But now you want to change your help text

from django.db import models

class TestModel(models.Model):
    name = models.CharField(
        max_length=50,
        verbose_name='Name',
        help_text='This is new help. Let\'s see what happens')

And makemigrations says:

$./manage.py makemigrations --dry-run --verbosity=3
Migrations for 'demo':
  0002_auto_20160528_1034.py:
    - Alter field name on testmodel
Full migrations file '0002_auto_20160528_1034.py':
# -*- coding: utf-8 -*-
# Generated by Django 1.9.6 on 2016-05-28 10:34
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

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

    operations = [
        migrations.AlterField(
            model_name='testmodel',
            name='name',
            field=models.CharField(help_text="This is new help. Let's see what happens", max_length=50, verbose_name='Name'),
        ),
    ]

You say "Whaaat? I haven't changed database structure! Why this migration appears here?"

And you are right. There is no any database changes here. Django just watches all changes. Let's change this behavior

  1. You need to change your import. Change
from django.db import models

to

from migration_control.db import models

They are the same and all your managers and function, that are in django, will continue working (Report bug if not).

  1. ./manage.py makemigrations. It will create new migration that maps django fields to components fields.
$./manage.py makemigrations --dry-run --verbosity=3
Migrations for 'demo':
  0002_auto_20160528_1039.py:
    - Alter field name on testmodel
Full migrations file '0002_auto_20160528_1039.py':
# -*- coding: utf-8 -*-
# Generated by Django 1.9.6 on 2016-05-28 10:39
from __future__ import unicode_literals

from django.db import migrations
import migration_control.db.models


class Migration(migrations.Migration):

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

    operations = [
        migrations.AlterField(
            model_name='testmodel',
            name='name',
            field=migration_control.models.CharField(max_length=50),
        ),
    ]

And there is no help text in migration! Now if you change your help_text no migrations are created. Enjoy

Third-party components

If tou want to use it with third-party models there is a way to do it. For example let's extend mptt-model:

from migration_control.mixins import NoMigrateMixin

class MPTTModel(NoMigrateMixin, mptt_models.MPTTModel):
    class Meta:
        abstract = True

Limitations

It works in Django 1.7 - 1.9.13, but I'm not shure it will work in next releases. Django migrations sometimes are changed, so double check migration, that changes your model fields - it shouldn't delete any of your fields or models, it should only change field types.

Python 2.7+ is required because of absolute_import and other _future_ functions

Versions

0.1.1 First wersion

About

Django migration control allows avoid unnecessary migrations such as changing help_text or verbose_name of fields

License:MIT License


Languages

Language:Python 100.0%