Py学习  »  问与答

这两个定义有什么区别

gcd0318 • 9 年前 • 7636 次点击  

例如,做一个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
 
7636 次点击  
文章 [ 4 ]  |  最新文章 9 年前
gcd0318
Reply   •   1 楼
gcd0318    9 年前

@alexkh 可以,但我想知道的是,就按照官方文档写,为什么会有这个提示,而且两个field完全一样,为什么另一个就能通过,以及最后的,我就那么复制粘贴再删除一下,怎么就又没问题了

alexkh
Reply   •   2 楼
alexkh    9 年前

设置default不行吗?

gcd0318
Reply   •   3 楼
gcd0318    9 年前

@Jason 试过别的值,试过两个都用1024,都不能过

不过现在,我找到解决的办法了

首先把https://docs.djangoproject.com/en/1.7/intro/tutorial01/这里的polls/models.py代码原样不动粘贴到我所有代码的最前面,然后makemigrations,通过,再把这部分代码删掉,再makemigrations,没问题了

别问我为什么,我也不知道

Jason
Reply   •   4 楼
Jason    9 年前

我觉得应该是content中的max_length太大了的原因。