Django PositiveIntegerField 中的 0 值?
models.PositiveIntegerField 类型的字段可以包含 0 值吗?我正在做类似的事情:
points = models.PositiveIntegerField()
谢谢,
我知道我应该自己尝试一下,但我这里没有 Django 环境。
Can a field of type models.PositiveIntegerField contain a 0 value? I'm doing something like:
points = models.PositiveIntegerField()
Thanks,
I know I should try it myself, but I haven't a Django environment here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,可以。是否应该存在争议 - 有一个长期存在的错误报告: http://code.djangoproject.com/门票/7609
Yes, it can. It is debatable whether it should--there is a longstanding bug report: http://code.djangoproject.com/ticket/7609
对于那些想要排除 0 的人,很容易编写 validator< /a> 为此。
然后这样使用它
For those looking to exclude the 0, it's easy to write a validator for that.
and then use it as such
是的。
模型字段引用是这么说的。为了完整起见,还在这里引用:
Yes.
The model field reference says so. For completeness, also quoted here:
根据正整数的定义,它不应该接受零值,但 django 实际上将其视为非负数,即包含零在内的数。所以,是的,它可以接受零值
Well by the definition of a Positive Integer, it shouldn't accept a zero value, but django actually considers it in the sense of none-negative number which is zero inclusive. So, Yes it can accept a zero value
是的,“PositiveIntegerField”可以包含“0”值。
例如,我定义了模型“Point”,如下所示:
然后,运行以下命令:
现在,我打开“db.sqlite3”,然后如您所见, "CHECK("points" >= 0)" 设置为"points" 字段,这意味着"PositiveIntegerField" 可以包含 “0”值:
Yes, "PositiveIntegerField" can contain "0" value.
For example, I defined the model "Point" as shown below:
Then, run this command below:
Now, I opened "db.sqlite3" then as you can see, "CHECK("points" >= 0)" is set to "points" field which means "PositiveIntegerField" can contain "0" value: