如何解决Django教程迁移错误?

时间:2015-01-20 12:06:24

标签: django

我正在使用本教程:

https://docs.djangoproject.com/en/1.7/intro/tutorial01/

vim mysite / settings.py

`INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls',
)`

vim polls / models.py

import datetime
from django.db import models
from django.utils import timezone

# Create your models here.
class Question(models.Model):
        question_text = models.CharField(max_length=200)
        pub_date = models.DateTimeField('date published')
        def __unicode__(self):
                return self.question_text

class Choice(models.Model):
        question = models.ForeignKey(Question)
        choice_text = models.CharField(max_length=200)
        votes = models.IntegerField(default=0)
        def __unicode__(self):
                return self.choice_text
        def was_published_recently(self):
                return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

`

使用教程中的所有代码后,我遇到了这个错误:

  

python manage.py makemigrations

You are trying to add a non-nullable field 'choice_text' to choice without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:

 1. Provide a one-off default now (will be set on all existing rows)
 2. Quit, and let me add a default in models.py
Select an option: 

我做错了什么?

1 个答案:

答案 0 :(得分:1)

嗯,实际上这不是错误。 Django告诉你它需要choice_text的默认值才能应用于存储在数据库中的当前行。似乎您创建了数据库表(可能包含syncdb),添加了一些数据,现在您必须强制使用choice_text字段。因此,Django必须使用您要求的新默认值填充数据库中choice_text的空实例。有意义吗?

只需选择选项1),然后在choice_text中输入要应用于数据库中所有当前行的值。

我希望它有所帮助。