这是我的模型:
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(*组) 文件“/home/wliao/Programming/MysteryLeague/src/controllers/test.py”,第 8 行,在 get 创建(100) 创建文件“/home/wliao/Programming/MysteryLeague/src/controllers/test.py”,第 18 行 文章 = 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 中 值 = self.validate(值) 文件“/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 行,在验证中 raise BadValueError('需要属性 %s' % self.name) BadValueError:需要属性内容
谢谢!
最佳答案
来自 the docs :
Because validation occurs when the instance is constructed, any property that is configured to be required must be initialized in the constructor.
所以:
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)
关于python - BadValueError : Property xxxx is required, 即使已经设置了 xxxx 属性? (谷歌应用引擎),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6142439/