Django 1.10 - makemigrations command not detecting

2019-05-14 21:16发布

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.

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.

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)

1条回答
The star\"
2楼-- · 2019-05-14 21:23

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.

查看更多
登录 后发表回答