Django 1.7 - makemigrations creating migration for unmanaged model

chadgh picture chadgh · Oct 28, 2014 · Viewed 7.2k times · Source

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:

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.

Answer

Daniel Hawkins picture Daniel Hawkins · Nov 6, 2014

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?