【发布时间】:2016-12-12 20:40:50
【问题描述】:
提前感谢您的帮助。在 mi 项目中,我有一个涉及从现有数据库生成的模型的应用程序。由于这些表由 DBA 管理,因此它们作为非托管模型保存。由于架构的更改,我们可能需要从 db 重新生成模型,因此我们为每个模型创建了替代代理模型,以将我们管理的部分与我们不管理的部分分离。下面你可以看到一个基于我们当前布局的示例。
该示例显示了一个生成模型与另一个生成模型的 FK,因此代理模型具有对非代理模型的引用。我已经阅读了指向here 的讨论并尝试了一些显示的方法,但是没有一个对我有用。所以现在我正在尝试更新生成的模型以指向代理模型,我认为这不会造成任何问题。
正如我所见,Django 为非托管模型生成了迁移,我认为 makemigration 会检测到该模型的 FK 中的更改。但是,当我运行manage.py makemigrations 时,它显示没有检测到任何更改。 这是非托管模型的预期行为吗?
# 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)
【问题讨论】:
标签: django django-models proxy-classes makemigrations