为列" uid"指定的多个默认值Django 1.8中的表格

时间:2015-11-28 08:56:57

标签: python django django-models django-admin

我正在创建Django Application,我的models.py是:

class Registration(models.Model):
    uid = models.AutoField(primary_key=True, default=0)
    uname = models.CharField(max_length=100, blank=False, null=False)
    upassword = models.CharField(max_length=100, blank=False, null=False)
    uphone = models.IntegerField(blank=True, null=True)
    uhid = models.ForeignKey('Hood', blank=False, null=False, default='ABC')
    uemail = models.EmailField(blank=False, null=False, default='abc402@nyu.edu')
    uintro = models.TextField(null=True, blank=True)
    uphoto = models.ImageField(upload_to='', blank=False, null=False, default='static/img/natural_join_is_inner_join.png')
    uhood = models.CharField(max_length=10, null=True)
    uaddress = models.CharField(max_length=100, default='ABC')
    # django automatically uses the media root which you have declared in your settings, define that to `upload_to`

    def __unicode__(self):
        return self.uname

然后我运行以下命令:

python manage.py makemigrations

python manage.py migrate

但是它显示了一个错误:

  

为列" uid"指定的多个默认值的表   " registration_registration"

有人可以帮我解决这个问题吗?堆栈溢出的其他链接没有多大帮助!

当我删除了id uid字段时,Django正在向我展示:

You are trying to add a non-nullable field 'id' to registration 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

问题是为什么我应该为django自动创建的id提供默认值?

更新:我删除了迁移文件夹,所以我解决了上述问题,但现在我收到错误:

  

"创建新内容类型时出错。请确保内容类型"   RuntimeError:创建新内容类型时出错。请确认   在尝试单独迁移应用程序之前,会迁移contenttypes。

1 个答案:

答案 0 :(得分:3)

您首先不应为default定义uid值。它是一个AutoField,它从数据库中获取一个自动递增的值。