如何将不需要的属性保存到数据存储区?

时间:2012-09-05 08:21:19

标签: python google-app-engine google-cloud-datastore

我有一个基本课程:

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!

它工作正常 - 创建实例并写入数据。但是一旦删除“required = true”属性就不再存储在数据存储区中:

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 docs中找到答案。

1 个答案:

答案 0 :(得分:1)

但您尚未删除required=True,您已删除了实例化该字段的括号()。它应该是

text = db.TextProperty()