BadValueError:即使已经设置了xxxx属性,属性xxxx也是必需的? (谷歌应用引擎)

时间:2011-05-26 17:13:55

标签: python google-app-engine web-applications

这是我的模特:

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行     handler.get(*基团)   文件“/home/wliao/Programming/MysteryLeague/src/controllers/test.py”,第8行,获取     创建(100)   在创建文件“/home/wliao/Programming/MysteryLeague/src/controllers/test.py”,第18行     article = models.model.Article()   在 init 中输入文件“/home/wliao/Programming/GoogleAppEngineSDK/google/appengine/ext/db/init.py”,第910行     道具设置(自我,价值)   在设置中输入文件“/home/wliao/Programming/GoogleAppEngineSDK/google/appengine/ext/db/init.py”,第594行     value = self.validate(value)   文件“/home/wliao/Programming/GoogleAppEngineSDK/google/appengine/ext/db/init.py”,第2627行,在验证中     value = super(UnindexedProperty,self).validate(value)   文件“/home/wliao/Programming/GoogleAppEngineSDK/google/appengine/ext/db/init.py”,第621行,在验证中     引发BadValueError('属性%s是必需'%self.name) BadValueError:属性内容是必需的

谢谢!

1 个答案:

答案 0 :(得分:7)

来自the docs

  

因为验证发生时   实例是构造的,任何属性   必须配置为必需的   在构造函数中初始化。

所以:

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)