1
我有一个基本类: 如何将不需要的属性保存到数据存储?
class Post (db.Model):
title = db.StringProperty(required = True)
text = db.TextProperty(required = True)
created = db.DateTimeProperty(auto_now_add = True).
new_record = Post(title = "TESSSST", text = "TESSST")
new_record.put() #works!
而且它工作正常 - 在创建实例和数据写入。但只要我删除“=需要真正的”属性不再存储在数据存储:
class Post (db.Model):
title = db.StringProperty(required = True)
text = db.TextProperty
created = db.DateTimeProperty(auto_now_add = True)
new_record = Post(title = "TESSSST")
new_record.text = "Burn conctructor!"
new_record.put() # text property won't be saved
我不知道这是否是有帮助的,但是当我通过检查所有print new_record.__dict__
值的属性,是由构造分配有'_'前缀(如'_title')和其他没有(只是'文本')。
所以问题是“是否有可能将数据存储保存为非必需的(而不是由构造函数初始化)?”我无法在gae文档中找到答案。
,你可以添加一个默认值,而不是无返回您可以设置默认=“” –
多么愚蠢的错误!非常感谢!你救了我的大脑) –