为什么在ndb中需要和默认是互斥的? [英] Why required and default are mutally exclusive in ndb?
问题描述
ValueError:重复,必需和默认是互斥的。
示例代码:
from google.appengine.ext从google.appengine.ext导入ndb
import db
$ b $ class NdbCounter(ndb.Model):
#引发ValueError
count = ndb.IntegerProperty(required = True,default = 1)
class DbCounter(db.Model):
#不会引发ValueError
count = db .IntegerProperty(required = True,default = 1)
我想实例化一个Counter而不必指定一个值。我也想避免有人把这个值覆盖到None。上面的例子被构造。我可能没有必要的属性生活,而是添加一个增量()方法。我仍然没有看到为什么required和default是互斥的原因。
它是一个错误还是一个特性?
我认为你是对的。也许我在写代码的那部分时感到困惑。 required = True的意思是不允许写入None,所以应该可以将其与默认值结合使用。请在NDB跟踪器中提交功能请求: http://code.google.com/p/ appengine-ndb-experiment / issues / list
注意,对于重复的属性,事情比较复杂,重复的可能会继续与要求的或者不相容的即使实现了上述功能。
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.
Sample code:
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)
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.
Is it a bug or a feature?
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
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中需要和默认是互斥的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!