Py学习  »  问与答

这两个定义有什么区别

gcd0318 • 10 年前 • 8475 次点击  

例如,做一个blog,定义如下

class Post(models.Model):
    title = models.CharField(max_length=4096)
    content = models.CharField(max_length=10240)

makemigrations的时候却出现

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

如果把content去掉,就能生成migration,但是去掉title就不会通过——也就是说问题是content引起的 但是这两个定义有什么区别吗?content难道有特殊作用

当然,可以加上default或者null来回避,但是为什么title可以而content不行?而且官方文档上定义CharField的时候也只有一个max_length而已

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/972
 
8475 次点击  
文章 [ 4 ]  |  最新文章 10 年前