为什么要在Django模型中为primarykey字段指定默认值

时间:2015-12-03 06:56:28

标签: python postgresql django-models

如果我们将字段指定为primary_key,那么为什么又要求为同一字段指定默认值。以下是我的模型结构。

class ScatterxBloggerProfile(models.Model):
    username = models.CharField(max_length=255L, primary_key=True)
    fullname = models.CharField(max_length=255L, null=False)
    profileimage = models.TextField(max_length=1000L, null=True, default=None)
    description = models.TextField()
    role = models.CharField(max_length=255L, blank=True)
    level = models.CharField(max_length=255L, null=False)
    since = models.CharField(max_length=255L, blank=True)
    speciality = models.CharField(max_length=1000L)
    writesabout = models.CharField(max_length=1000L)
    fbhandle = models.CharField(max_length=255L, unique=True, null=False)
    twthandle = models.CharField(max_length=255L, unique=True, null=False)
    lkdhandle = models.CharField(max_length=255L, unique=True, null=False)
    distributionavailable = models.BooleanField(default=False)

我之前创建了这个表,几个字段缺席,django默认ID为primary_key。现在我要添加一个新的主键。当我尝试进行迁移时,它会问我:

? The field 'ScatterxBloggerProfile.username' does not have a default specified, yet is NOT NULL. ? Since you are adding this field, you MUST specify a default ? value to use for existing rows. Would you like to: ? 1. Quit now, and add a default to the field in models.py ? 2. Specify a one-off value to use for existing columns now ? Please select a choice: 为什么要指定默认值?

0 个答案:

没有答案