Django 1.7 - makemigrations creating migration for unmanaged model

落爺英雄遲暮 提交于 2019-12-03 09:48:15

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?

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
        )

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!