Django 1.10-makemigrations命令未检测到非托管模型的更改 [英] Django 1.10 - makemigrations command not detecting changes for unmanaged models

查看:121
本文介绍了Django 1.10-makemigrations命令未检测到非托管模型的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此先感谢您的帮助.在mi项目内部,我有一个涉及从现有数据库生成的模型的应用程序.由于这些表是由DBA管理的,因此将它们保留为非托管模型.由于可能由于架构的更改而需要从db重新生成模型,因此我们为这些模型中的每一个都创建了替代代理模型,以将我们管理的部分与不需要的部分分离.您可以在下面看到基于我们当前布局的示例.

Thanks in advance for your help. Inside mi project I have an app involving models that were generated from an existing database. As those tables are administered by a DBA, they are kept as unmanaged models. As it is possible that we require to re-generate the models from db due to changes in the schema, we have created alternative proxy models for each of those models to decouple the part that we manage from what we don't. Below you can see an example based on our current layout.

该示例显示了一个具有FK的生成的模型到另一个生成的模型,因此,代理模型具有对非代理模型的引用.我已经阅读了在此处所指向的讨论,并尝试了所示的一些方法,但是这些方法都不适合我.因此,现在我正在尝试更新生成的模型以指向代理模型,我认为这不会引起任何问题.

The example shows a generated model with a FK to another generated model, accordingly the proxy model has a reference to a non proxy model. I have read the discussion pointed here and tried some of the approaches shown, however None of them has worked for me. So right now I'm trying to update the generated model to point to the proxy one, which I think should not cause any problem.

正如我所见,Django为非托管模型生成了迁移,我认为makemigration可以检测到该模型的FK中的更改.但是,当我运行manage.py makemigrations时,它表明未检测到任何更改. 对于非托管模型,makemigrations的预期行为是吗?

As I have seen that Django generated a migration for the unmanaged models, I thought that makemigration would detect the change in the FK for that model. However, when I run manage.py makemigrations It shows that no changes were detected. Is it the expected behaviour of makemigrations for unmanaged models?

# app/models.py
class SacLocation(models.Model):
    sacloc_location_id = models.IntegerField(primary_key=True)
    sacloc_name = models.CharField(max_length=50, blank=True, null=True)
    sacloc_state = models.IntegerField(blank=True, null=True)

    # I'm changing this Field to point to the proxy model
    # e.g. it will look like this, but the change is not detected by makemigrations
    # sacloc_location_grouping = models.ForeignKey('LocationGroupingProxy', 
    #            models.DO_NOTHING, db_column='sacloc_location_grouping')
    sacloc_location_grouping = models.ForeignKey('SacLocationGrouping', 
                 models.DO_NOTHING, db_column='sacloc_location_grouping')

    class Meta:
        managed = False
        db_table = 'sac_location'


class SacLocationGrouping(models.Model):
    saclgr_location_grouping__id = models.IntegerField(primary_key=True)
    saclgr_name = models.CharField(max_length=50, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'sac_location_grouping'


class LocationProxy(SacLocation):        
    class Meta:
        proxy = True

    def __str__(self):
        return u'%s' % (self.sacloc_name)


class LocationGroupingProxy(SacLocationGrouping):
    class Meta:
        proxy = True

    def __str__(self):
        return u'%s' % (self.saclgr_name)

推荐答案

我在代码中进行了几处更改,以将非托管模型(原来是FK指向其他非托管模型)指向代理模型.这些更改均未导致生成新的迁移,因此我想预期的行为就是这种情况.查看了Django源代码,但未能发现该检测到的地方.最后,当我对代理模型中的Meta选项(例如订购)进行更改时,Django实际上检测到了更改并创建了新的迁移.

I have made several changes in my code to point unmanaged models, which originally FK to other unmanaged models, to proxy models. None of those changes have caused a new migration to be generated so I suppose that the expected behaviour is that in this case. Looked at the Django source code but failed to spot the place were this changes are detected. Finally, when I made changes to the Meta options (e.g. ordering) in proxy models, Django actually detected the changes and created a new migration.

这篇关于Django 1.10-makemigrations命令未检测到非托管模型的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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