问题描述
在旧的 google appengine 数据存储 API 中,必需"和默认"可以一起用于属性定义.使用 ndb 我得到一个
In old google appengine datastore API "required" and "default" could be used together for property definitions. Using ndb I get a
ValueError: repeated, required and default are mutally exclusive.
示例代码:
from google.appengine.ext import ndb
from google.appengine.ext import db
class NdbCounter(ndb.Model):
# raises ValueError
count = ndb.IntegerProperty(required=True, default=1)
class DbCounter(db.Model):
# Doesn't raise ValueError
count = db.IntegerProperty(required=True, default=1)
我想实例化一个计数器而不必指定一个值.我也想避免有人将该值覆盖为 None.上面的例子是构建的.我可能没有必需的属性,而是添加一个 increment() 方法.我仍然不明白为什么 required 和 default 是相互排斥的.
I want to instantiate a Counter without having to specify a value. I also want to avoid someone to override that value to None. The example above is constructed. I could probably live without a required attribute and instead add an increment() method. Still I don't see the reason why required and default are mutually exclusive.
这是错误还是功能?
推荐答案
我认为你是对的.也许我在编写那部分代码时感到困惑.required=True"意味着不允许写入值 None"是有道理的,因此应该可以将其与默认值结合使用.请在 NDB 跟踪器中提交功能请求:http://code.google.com/p/appengine-ndb-experiment/issues/list
I think you are right. Perhaps I was confused when I write that part of the code. It makes sense that "required=True" means "do not allow writing the value None" so it should be possible to combine this with a default value. Please file a feature request in the NDB tracker: http://code.google.com/p/appengine-ndb-experiment/issues/list
请注意,对于重复的属性,事情更复杂,即使实现了上述功能,重复也可能会继续与 required 或 default 不兼容.
Note that for repeated properties things are more complicated, to repeated will probably continue to be incompatible with either required or default, even if the above feature is implemented.
这篇关于为什么 ndb 中的 required 和 default 是互斥的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!