BadValueError:即使已设置xxxx属性,xxxx属性仍然是必需的?(谷歌应用引擎)
这是我的模型:
from google.appengine.ext import db
from google.appengine.ext.db import polymodel
class Item(polymodel.PolyModel):
title = db.StringProperty(required=True)
summary = db.StringProperty(required=True)
content = db.TextProperty(required=True)
createDate = db.DateTimeProperty(auto_now_add=True)
class Article(Item):
author = db.StringProperty()
这是我的处理器:
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import models.model
class Test(webapp.RequestHandler):
def get(self):
create(100)
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Test')
self.response.out.write('<p>Created')
app = webapp.WSGIApplication([('/test/*', Test)], debug=True)
def create(count):
for i in range(0,count,1):
article = models.model.Article()
article.title = "Test title " + str(i)
article.author = "wliao"
article.summary = "this is a test " + str(i)
article.content = "this is the content of the article"
article.put()
def main():
run_wsgi_app(app)
if __name__ == "__main__":
main()
我想问的是,我已经设置了必要的属性,为什么在浏览器中加载时还是会出现这个错误:
错误追踪(最近的调用在最前面): 文件 "/home/wliao/Programming/GoogleAppEngineSDK/google/appengine/ext/webapp/init.py",第 700 行,在 call handler.get(*groups) 文件 "/home/wliao/Programming/MysteryLeague/src/controllers/test.py",第 8 行,在 get create(100) 文件 "/home/wliao/Programming/MysteryLeague/src/controllers/test.py",第 18 行,在 create article = models.model.Article() 文件 "/home/wliao/Programming/GoogleAppEngineSDK/google/appengine/ext/db/init.py",第 910 行,在 init prop.set(self, value) 文件 "/home/wliao/Programming/GoogleAppEngineSDK/google/appengine/ext/db/init.py",第 594 行,在 set value = self.validate(value) 文件 "/home/wliao/Programming/GoogleAppEngineSDK/google/appengine/ext/db/init.py",第 2627 行,在 validate value = super(UnindexedProperty, self).validate(value) 文件 "/home/wliao/Programming/GoogleAppEngineSDK/google/appengine/ext/db/init.py",第 621 行,在 validate raise BadValueError('属性 %s 是必需的' % self.name) BadValueError: 属性 content 是必需的
谢谢!
1 个回答
来自文档:
因为在创建实例的时候会进行验证,所以任何被设置为必需的属性必须在构造函数中进行初始化。
所以:
title = "Test title " + str(i)
author = "wliao"
summary = "this is a test " + str(i)
content = "this is the content of the article"
article = models.model.Article(title=title, author=author,
summary=summary, content=content)