2014-11-25 55 views
3

为可选字段提供默认值,我写了一个Python模型下面给出:从django.db进口车型在Django模型

class Product(models.Model): 


     title = models.CharField(max_length=255, unique=True) 
     description = models.TextField(blank=True) 
     image_url = models.URLField(blank=True) 
     quantity = models.PositiveIntegerField(default=0) 

     def sell(self): 

       self.quantity = self.quantity - 1 
       self.save() 
       return self.quantity 

当我试图创建使用迁移模式

,我得到以下信息:

You are trying to add a non-nullable field 'description' to product 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: 

我的问题是,如果我的“说明”设置“空白=真”,是有必要指定字段的默认值?还是我错过了别的?

回答

2

为Django 1.7的此行为创建了一张票。 看一看here

+0

ohh ...但是我需要在其他地方使用那个模式.. :( – user3033194 2014-11-25 12:43:39

+0

现在你可以手动输入默认的'',当这个错误被修复时,它只会自动执行,没有其他任何东西。 – RemcoGerlich 2014-11-25 12:59:12

3

blank=Truenull=True不一样,作为the documentation explains。当文本字段为空时,它仍然需要某种值:但该值可以是空字符串。

所以,只需选择选项1,并输入''作为默认值。

+0

好的,我明白了,'blank = True'仅用于该字段,而'null = True'用于字段和数据库条目。是的,架构已经创建,感谢您清除我的疑惑! – user3033194 2014-11-25 12:50:02