python - BadValueError : Property xxxx is required, 即使已经设置了 xxxx 属性? (谷歌应用引擎)

标签 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 行,在 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/

相关文章:

asp.net - 将 RDL 报告加载到 Web 报告查看器中

python - 在 Raspbian Buster 上将 Python 3.7 降级到 3.5

python - 如何获取从5号开始的前9个字符?

python - 如何正确安装Open edX电子商务服务?

java - JSTL Google 应用引擎问题

java - 检测 url 是否可以使用 HTTP 请求访问

解释器环境中的python垃圾收集和_下划线

java - 延迟任务响应

android - 如何将设备上运行的 android 应用程序连接到本地 Web 应用程序?

html - cordova-sqlite-ext 可以读取 SD 卡(Android)上的数据库吗?