“BadValueError: Property category is required” on

2019-03-06 09:59发布

I am trying to make a simple webapp html page that prints out data given from a datastore. However, I am continually running the following error:

    raise BadValueError('Property %s is required' % self.name)
BadValueError: Property category is required

I have heard that this is because I must initialize my properties beforehand but as of yet, have not found an appropriate way to do this.

The following is placed in model.py

class Question(db.Model):
  category = db.StringProperty(required=True)
  question = db.StringProperty(required=True, multiline=True)
  creator = db.StringProperty(required=True, multiline=True)
  answer = db.StringProperty(required=True, multiline=True)
  mustHave = db.StringProperty(required=False, multiline=True)
  group = db.StringProperty(required=False)

The following is given on a separate pages.py

class SpPage(webapp.RequestHandler):
    def printPage(self,path):
        user = users.get_current_user()
        template_values = getCommonValues(user)

        if user:
            template_values['questions'] = model.Question.all().fetch(100)
            self.response.out.write(template.render(path, template_values))
        else:
            path = os.path.join(os.path.dirname(__file__), 'html/pleaseLogin.html')
            self.response.out.write(template.render(path, template_values))

My html page is as follows:

Questions<br/>
{% for eachQ in questions %}
    <p>
    <a href='/doQuestionPage?id={{eachQ.key}}'>{{eachQ.question}}</a><br/>
    by {{eachQ.creator}}
</p>
{% endfor %}

The class to add data to datastore: Note: this is attached to a form which posts data using this class. Not all code is in there so don't worry if some variables such as "something" don't appear to be used - they are. It seems to be working thus far.

class AddQuestion(webapp.RequestHandler):
  def doPost(self,something):        
    user = users.get_current_user()
    template_values = getCommonValues(user)

    c = self.request.get('cat')
    q = self.request.get('question')
    a = self.request.get('answer')
    m = self.request.get('musthaves')

    if user:
        emailStr = user.email().lower()
        if q and a and m:
            newQuestion = model.Question(category = c, question = q, creator = emailStr, answer = a, mustHave = m)
            newQuestion.put()
            template_values['message'] = 'New question created!'

1条回答
倾城 Initia
2楼-- · 2019-03-06 10:20

There might be some entities in your current datastore that don't have the category property, or other required properties, filled out. This could sometimes happen when you add the property to your model after already creating some other entities before that. Or perhaps adding the required=True option to a property that previously wasn't required.

If you are playing with dev data, I would suggest that you either clear your datastore or delete all your Question entities, and see if that works.

Otherwise, you'd have to manually add data to all the required fields or remove the required=True option.

查看更多
登录 后发表回答