1

我有以下型号 -

class ToDo(models.Model):

    todo_title = models.CharField(null=True, blank=True, max_length=200)
    todo_status = models.IntegerField(choices=TASK_STATUS, null=True, blank=True)
    assigned_to = models.ManyToManyField(OrgStaff, null=True, blank=True, related_name='assigned_to')
    assigned_by = models.ManyToManyField(OrgStaff, null=True, blank=True, related_name='assigned_by')
    assigned_time = models.DateTimeField(auto_now_add=True)
    completed_time = models.DateTimeField(null=True, blank=True)

然后我运行python manage.py convert_to_south todoapptodoapp是应用程序的名称然后我跑python manage.py migrate todoapp.

完成后,我在上述模型中添加另一个字段 -

class ToDo(models.Model):

    todo_title = models.CharField(null=True, blank=True, max_length=200)
    todo_slug = models.SlugField(null=True, blank=True)
    todo_status = models.IntegerField(choices=TASK_STATUS, null=True, blank=True)
    assigned_to = models.ManyToManyField(OrgStaff, null=True, blank=True, related_name='assigned_to')
    assigned_by = models.ManyToManyField(OrgStaff, null=True, blank=True, related_name='assigned_by')
    assigned_time = models.DateTimeField(auto_now_add=True)
    completed_time = models.DateTimeField(null=True, blank=True)

现在我进行模式迁移-python manage.py schemamigration todoapp --auto然后python manage.py migrate todoapp这样做会出现以下错误-

Running migrations for taskbase:
 - Migrating forwards to 0002_auto__add_field_todo_todo_slug.
 > taskbase:0002_auto__add_field_todo_todo_slug
KeyError: u'todo_title'

知道为什么我会收到此错误吗? 我敲了敲头,却找不到原因。

4

1 回答 1

0

使用 'todo_' 作为前缀的 ' title ' 和 ' status ' 可能会导致与表字段的名称发生冲突。事实上,在数据库中,Django 正在命名字段 todoapp_todo_todo_status,而 South 可能只是被混淆了。South在内部做了一些创造性的事情,因此发生了冲突。我建议尝试:

class ToDo(models.Model):

    title = models.CharField(null=True, blank=True, max_length=200)
    status = models.IntegerField(choices=TASK_STATUS, null=True, blank=True)
    assigned_to = models.ManyToManyField(OrgStaff, null=True, blank=True, related_name='assigned_to')
    assigned_by = models.ManyToManyField(OrgStaff, null=True, blank=True, related_name='assigned_by')
    assigned_time = models.DateTimeField(auto_now_add=True)
    completed_time = models.DateTimeField(null=True, blank=True)

我想变得迂腐,我还可以指出todoapp应该称为todos,但这对您的项目没有影响。

于 2013-08-19T18:14:16.730 回答