为可选字段提供默认值,我写了一个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:
我的问题是,如果我的“说明”设置“空白=真”,是有必要指定字段的默认值?还是我错过了别的?
ohh ...但是我需要在其他地方使用那个模式.. :( – user3033194 2014-11-25 12:43:39
现在你可以手动输入默认的'',当这个错误被修复时,它只会自动执行,没有其他任何东西。 – RemcoGerlich 2014-11-25 12:59:12