虽然我仍然很想知道这种方法是否正确,但我能够弄清楚如何仅通过两次迁移/提交来执行上述计划。
首先,我new_grade = models.SmallIntegerField(choices=(1, 2, 3))
在模型中添加了一个字段(这需要复制枚举变量),并更新了模型类的和字段中对grade
to的引用:new_grade
ordering
unique_together
Meta
class Foo(models.Model):
A, B, C = 'A', 'B', 'C'
A2, B2, C2, = 1, 2, 3
grade = models.CharField(max_length='1', choices=((A, 'A'), (B, 'B'), (C, 'C')))
new_grade = models.SmallIntegerField(choices=((A2, 1), (B2, 2), (C2, 3)))
class Meta:
ordering = ['x', 'new_grade']
unique_together = ('x', 'new_grade')
运行后manage.py schemamigration app --auto
打开迁移文件,修改forward方法为:
def forwards(self, orm):
# For the unique_together...
db.delete_unique('app_foo', ['x', 'grade'])
db.add_column('app_foo', 'new_grade',
self.gf('django.db.models.fields.SmallIntegerField')(default=1),
keep_default=False)
if not db.dry_run:
mapping = {'A': 1, 'B': 2, 'C': 3}
for foo in orm.Foo.objects.all():
foo.new_grade = mapping[foo.grade]
foo.save()
# For the unique_together...
db.create_unique('app_foo', ['x', 'new_grade'])
运行后manage.py migrate app
,所有 Foos 现在都有一个带有映射值的重复 new_grade 字段。那时我提交了我的代码,因为它处于稳定状态。
其次,在models.py中,我删除了旧grade
字段,重命名了重复的枚举变量,并再次更新了new_grade
类中的引用Meta
:
class Foo(models.Model):
A, B, C, = 1, 2, 3
grade = models.SmallIntegerField(choices=((A, 1), (B, 2), (C, 3)))
class Meta:
ordering = ['x', 'grade']
unique_together = ('x', 'grade')
我再次运行manage.py schemamigration app --auto
并打开迁移文件将转发方法修改为:
def forwards(self, orm):
# For the unique_together...
db.delete_unique('app_foo', ['x', 'new_grade'])
db.delete_column('app_foo', 'grade')
db.rename_column('app_foo', 'new_grade', 'grade')
# For the unique_together...
db.create_unique('app_foo', ['x', 'grade'])
运行后manage.py migrate app
,所有 Foo 的grade
字段都替换为原来的new_grade
字段,迁移完成!