我使用python在webapp2框架中工作,我有一个父类(Examplestart),它有三个属性:
variable = ndb.KeyProperty(kind=DBUser)
time = ndb.DateTimeProperty(auto_now_add=True)
statement = ndb.StringProperty(indexed=False)
该示例有两个子类(Example1& Example2),我希望在Examplestart中对属性进行硬编码,以便:
my_example = DBExamplestart(
variable = '776',
statement = ['First']
)
my_example.put()
我应该把这段代码插入到我的文件中吗?如果我在Examplestart中使用它,代码就不起作用了。my_example与Example1类相关。
发布于 2015-04-15 01:54:09
可以在子实体中设置默认值:
class DBExamplestart(Examplestart):
variable = ndb.KeyProperty(kind=DBUser, default=ndb.Key(DBUser, 776))
statement = ndb.StringProperty(indexed=False, default='First')
如果您有更复杂的事情要做(比如检索特定的id,而不是776),您可以查看模型钩。例如,_pre_put_hook()将允许您在持久化实体之前对其执行任何操作。
https://stackoverflow.com/questions/29645046
复制