[Django]-Django 1.7 – makemigrations creating migration for unmanaged model

5👍

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?

3👍

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
        )

1👍

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.

Leave a comment