我的解决方案如下。首先,添加 Feline 模型,但 Cat 模型保持不变:
class Feline(models.Model):
objects = models.Manager()
class Cat(models.Model):
objects = models.Manager()
name = models.CharField(max_length=255)
miaow_factor = models.IntegerField()
接下来,创建并运行架构迁移 (manage.py schemamigration animals --auto),以便将 Feline 模型添加到数据库中。
接下来,创建一个数据迁移 (manage.py datamigration animals cat_feline)。在数据迁移中,添加代码为每只猫创建一只猫,这样创建的每只猫都与一只猫共享一个 ID。此外,更改新猫科动物的顺序,以便为所有新猫科动物分配大于当前最大猫 ID 的 ID。
class Migration(DataMigration):
def forwards(self, orm):
#create a Feline for each Cat
for c in orm['Cat'].objects.all():
f = orm['Feline']()
f.id = c.id
f.save()
if orm['Feline'].objects.count():
#if there are any Feline objects, make sure that new ids are allocated after the largest current ID
last_id = orm['Feline'].objects.latest('id').id
db.execute('alter sequence animals_feline_id_seq restart with %s;' % (last_id + 1))
def backwards(self, orm):
#no need to do anything if migrating backwards
pass
接下来,更改模型文件以使 Cat 继承自 Feline,并将 OneToOneField 添加到 Cat,这将是 Cats 的新主键:
class Feline(models.Model):
objects = models.Manager()
class Cat(Feline):
#n.b. Cat now inherits from Feline, not models.Model
objects = models.Manager()
feline = models.OneToOneField(Feline, parent_link = True)
name = models.CharField(max_length=255)
miaow_factor = models.IntegerField()
接下来,创建另一个架构迁移,以便将这些更改应用到数据库。但是,不要运行迁移。相反,更改迁移中的代码以将Cat.id 列重命名为Cat.feline_id
class Migration(SchemaMigration):
def forwards(self, orm):
#original changes generated by South:
# Deleting field 'Cat.id'
#db.delete_column(u'animals_cat', u'id')
# Adding field 'Cat.feline'
#db.add_column(u'animals_cat', 'feline',
# self.gf('django.db.models.fields.related.OneToOneField')(default=None, to=orm['animals.Feline'], unique=True, primary_key=True),
# keep_default=False)
#instead of doing the above, just rename Cat.id to Cat.feline_id
#and drop the default numbering sequence for the Cat.feline_id field
db.rename_column('animals_cat', 'id', 'feline_id')
db.execute("ALTER TABLE animals_cat ALTER COLUMN feline_id DROP DEFAULT")
def backwards(self, orm):
#original changes generated by South:
# Adding field 'Cat.id'
#db.add_column('animals_cat', u'id',
# self.gf('django.db.models.fields.AutoField')(default=None, primary_key=True),
# keep_default=False)
# Deleting field 'Cat.feline_id'
#db.delete_column(u'animals_cat', 'feline_id')
#instead of doing the above, rename Cat.feline_id to Cat.id
#and reinstate the default numbering sequence for the Cat.id field
db.rename_column('animals_cat', 'feline_id', 'id')
db.execute("ALTER TABLE animals_cat ALTER COLUMN id SET DEFAULT nextval('u'animals_cat_id_seq'::regclass)")
if orm['Cat'].objects.count():
#if there are any Cat objects, make sure that new ids are allocated after the largest current ID
last_id = orm['Cat'].objects.latest('id').id
db.execute('alter sequence animals_cat_id_seq restart with %s;' % (last_id + 1))
最后,运行刚刚编辑的架构迁移,就大功告成了。
现在,如果您愿意,您可以使用进一步的架构迁移和数据迁移轻松地将一些字段(例如名称)从 Cat 移动到 Feline。
如果您想为多个现有模型创建一个超类(在这种情况下,您可能无法为所有实例保留相同的 ID,那么幸运的是,我不必处理)进一步的挑战,因为两个不同子类中的某些实例可能会在 ID 上发生冲突。欢迎任何关于如何解决此问题的想法。