Django 1.7 - 为非托管模式创建迁移的makemigrations [英] Django 1.7 - makemigrations creating migration for unmanaged model

查看:155
本文介绍了Django 1.7 - 为非托管模式创建迁移的makemigrations的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中创建了一些动态的Django模型,除了迁移系统之外,所有的东西似乎都按预期工作。



如果我创建一个动态的Django模型, set managed = False,Django的 makemigrations 命令仍会为该新模型生成迁移。迁移看起来像这样:

  class Migration(migrations.Migration):

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

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

如果我不创建迁移,当我运行 python manage.py migrate 时,我看到以下消息(以惊人的红色字母):

 您的模型具有尚未反映在迁移中的更改,因此不会应用。 
运行'manage.py makemigrations'进行新的迁移,然后重新运行'manage.py migrate'以应用它们。

有没有办法告诉Django 1.7中的迁移系统一起忽略非托管模型?或者可能在模型的Meta类中设置 migrations = False



更新:为了澄清,我使用一种方法创建类似于以下地方描述的动态模型:





此方法非常适合基于我的配置模型中存储的信息生成动态模型( https://code.djangoproject.com/wiki/DynamicModels#Adatabase-drivenapproach )。我确实必须注册一个信号来清除django模型缓存,以便在更改配置实例时捕获对模型的更改,但除了为这些模型生成迁移之外,所有内容似乎都很有用。如果我删除其中一个配置,并从Django的缓存中删除该模型,则迁移将需要重新更新,删除不应该关心的模型。



这些动态模型没有在应用程序中使用。我在代码中没有指的是书籍模型(从上面的例子)。它们在运行时生成,用于从他们提供访问的遗留表读取信息。

解决方案

简单的答案是Django不是为此建造的。使您的模型不受管理只表示Django不会创建或删除表对于它 - 没有别的



这就是说,如果你在同一个应用程序中没有这些动态模型的常规模型,你可以有条件地添加应用程序到 INSTALLED_APPS settings.py 中:

 如果没有(sys.argv中的'makemigrations'或sys.argv中的'迁移'):
INSTALLED_APPS + =(
'app_with_dynamic_models',
'another_app_with_dynamic_models' ,

这应该使Django在创建和运行迁移时忽略该应用程序。但是,如果您想要使用模型,则最终必须制作和运行模型,因为具有不使用迁移的应用程序的能力意味着在Django 1.9中消失。您的动态模型是否可以重构使用内容类型框架


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.

解决方案

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?

这篇关于Django 1.7 - 为非托管模式创建迁移的makemigrations的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆