BadValueError:即使已经设置了xxxx属性,还是需要属性xxxx? Google App引擎

问题描述

| 这是我的模型:
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()
我的问题是,我已经设置了必需的属性,为什么在浏览器中加载该错误仍会出现此错误: 追溯(最近一次通话):   通话中第700行的文件\“ / home / wliao / Programming / GoogleAppEnginesDK / google / appengine / ext / webapp / init.py \”     handler.get(* groups)   在获取文件\“ / home / wliao / Programming / MysteryLeague / src / controllers / test.py \”,第8行     创建(100)   在创建的文件\“ / home / wliao / Programming / MysteryLeague / src / controllers / test.py \”,第18行     文章= models.model.Article()   初始化时,文件\“ / home / wliao / Programming / GoogleAppEnginesDK / google / appengine / ext / db / init.py \”,第910行     prop.set(自我,价值)   文件\“ / home / wliao / Programming / GoogleAppEnginesDK / google / appengine / ext / db / init.py \”,在第594行中     值= self.validate(值)   验证文件\“ / home / wliao / Programming / GoogleAppEnginesDK / google / appengine / ext / db / init.py \”,第2627行     值=超级(UnindexedProperty,自身)。验证(值)   验证文件\“ / home / wliao / Programming / GoogleAppEnginesDK / google / appengine / ext / db / init.py \”,行621     引发BadValueError(\'属性%s是必需的\'%self.name) BadValueError:属性内容是必需的 谢谢!     

解决方法

从文档:   因为验证发生在   实例被构造,任何属性   必须配置为必须   在构造函数中初始化。 所以:
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)