Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 1.7 - makemigrations creating migration for unmanaged model

I am creating some dynamic Django models in my application and everything seems to be working as expected except for the migration system.

If I create a dynamic Django model and set managed = False, Django's makemigrations command still generates a migration for that new model. The migration looks something like this:

class Migration(migrations.Migration):

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

    operations = [
        migrations.CreateModel(
            name='books',
            fields=[
            ],
            options={
                'db_table': 'books',
                'managed': False,
            },
            bases=(models.Model,),
        ),
    ]

If I don't create the migration, when I run python manage.py migrate, I see the following message (in big scary red letters):

Your models have changes that are not yet reflected in a migration, and so won't be applied.
Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.

Is there a way to tell the migrations system in Django 1.7 to ignore unmanaged models all together? or perhaps a migrations = False setting in the Meta class of the models?

UPDATE: for clarification, I am using a method to create my dynamic models similar to the ones describe in the following places:

  • http://dynamic-models.readthedocs.org/en/latest/topics/model.html#topics-model
  • https://code.djangoproject.com/wiki/DynamicModels

This method is great for generating my dynamic models based on information stored in my Configuration models (https://code.djangoproject.com/wiki/DynamicModels#Adatabase-drivenapproach). I did have to register a signal to clear the django model cache to catch changes to the models when a Configuration instance is changed, but everything seems to be working great, except for the fact that migrations are generated for these models. If I delete one of the configurations and the model is deleted from Django's cache, the migration would need to be updated again, removing the model that it shouldn't care about.

These dynamic models are not used in the application specifically. No where in the code do I refer to a books model (from the example above). They are generated at runtime and used to read information from the legacy tables they provide access to.

like image 522
chadgh Avatar asked Oct 28 '14 18:10

chadgh


People also ask

What is the difference between Makemigrations and migrate in Django?

makemigrations is responsible for packaging up your model changes into individual migration files - analogous to commits - and migrate is responsible for applying those to your database.

How does Django Makemigrations work?

Django uses a database table called django_migrations . Django automatically creates this table in your database the first time you apply any migrations. For each migration that's applied or faked, a new row is inserted into the table. As you can see, there is an entry for each applied migration.


3 Answers

The short answer is that Django is not built for this. Making your model "unmanaged" only means Django will not create or delete the table for it -- nothing else.

That said, if you have no regular models alongside these dynamic models in the same app, you can conditionally add the app to INSTALLED_APPS in settings.py:

if not ('makemigrations' in sys.argv or 'migrate' in sys.argv):
    INSTALLED_APPS += (
        'app_with_dynamic_models',
        'another_app_with_dynamic_models',
    )

This should make Django ignore the app when creating and running migrations. However, you will eventually have to make and run migrations for the models if you want to use them, since the ability to have apps which do not use migrations is meant to go away in Django 1.9. Could your dynamic models be refactored to use the contenttypes framework?

like image 68
Daniel Hawkins Avatar answered Oct 20 '22 14:10

Daniel Hawkins


I suggest you replace the generated migrations.CreateModel operation by one of your own that always reflect the actual model state. This way no state changes should be ever detected.

class CreateDynamicModel(CreateModel):
    def __init__(self):
        # ... dynamically generate the name, fields, options and bases
        super(CreateDynamicModel, self).super(
            name=name, fields=fields, options=optins, bases=bases
        )
like image 2
Simon Charette Avatar answered Oct 20 '22 14:10

Simon Charette


You can probably write a custom database router with the allow_migrate method returning False for your dynamic models. The migrate command will disallow them in that case.

As long as you don't load these dynamic models in any models.py module, makemigrations shouldn't pick them up either.

like image 1
meshantz Avatar answered Oct 20 '22 16:10

meshantz