BadValueError: Property xxxx is required, even aft

2019-06-20 15:26发布

Here's my model:

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

and my handler:

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

My question is, I already set the required properties, why do I still get this error when loaded it in the browser:

Traceback (most recent call last): File "/home/wliao/Programming/GoogleAppEngineSDK/google/appengine/ext/webapp/init.py", line 700, in call handler.get(*groups) File "/home/wliao/Programming/MysteryLeague/src/controllers/test.py", line 8, in get create(100) File "/home/wliao/Programming/MysteryLeague/src/controllers/test.py", line 18, in create article = models.model.Article() File "/home/wliao/Programming/GoogleAppEngineSDK/google/appengine/ext/db/init.py", line 910, in init prop.set(self, value) File "/home/wliao/Programming/GoogleAppEngineSDK/google/appengine/ext/db/init.py", line 594, in set value = self.validate(value) File "/home/wliao/Programming/GoogleAppEngineSDK/google/appengine/ext/db/init.py", line 2627, in validate value = super(UnindexedProperty, self).validate(value) File "/home/wliao/Programming/GoogleAppEngineSDK/google/appengine/ext/db/init.py", line 621, in validate raise BadValueError('Property %s is required' % self.name) BadValueError: Property content is required

Thanks!

1条回答
家丑人穷心不美
2楼-- · 2019-06-20 16:14

From the docs:

Because validation occurs when the instance is constructed, any property that is configured to be required must be initialized in the constructor.

So:

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)
查看更多
登录 后发表回答