【问题标题】:BadValueError: Property xxxx is required, even after the xxxx property has already been set? (google app engine)BadValueError: 属性 xxxx 是必需的,即使在 xxxx 属性已经设置之后? (谷歌应用引擎)
【发布时间】:2011-09-02 19:14:07
【问题描述】:

这是我的模型:

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()

我的问题是,我已经设置了所需的属性,为什么我在浏览器中加载它时仍然会出现这个错误:

Traceback(最近一次调用最后一次): call 中的文件“/home/wliao/Programming/GoogleAppEngineSDK/google/appengine/ext/webapp/init.py”,第 700 行 handler.get(*groups) 文件“/home/wliao/Programming/MysteryLeague/src/controllers/test.py”,第 8 行,在 get 创建(100) 文件“/home/wliao/Programming/MysteryLeague/src/controllers/test.py”,第 18 行,在创建中 文章 = 模型.model.Article() init 中的文件“/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 行,验证中 value = super(UnindexedProperty, self).validate(value) 文件“/home/wliao/Programming/GoogleAppEngineSDK/google/appengine/ext/db/init.py”,第 621 行,验证中 raise BadValueError('需要属性 %s' % self.name) BadValueError: 属性内容是必需的

谢谢!

【问题讨论】:

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


    【解决方案1】:

    来自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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-26
      • 1970-01-01
      • 2020-09-16
      • 2016-01-18
      • 2020-02-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多