如何解决这个Python BadValueError('属性%s是必需'%self.name)

时间:2012-08-09 01:13:50

标签: python google-app-engine google-cloud-datastore

我有这堂课:

class Contract(db.Model):
    book_number = db.IntegerProperty(required = True)
    initial_page = db.IntegerProperty(required = True)
    final_page = db.IntegerProperty(required = True)
    contract_type = db.StringProperty(required = True)
    parties = db.ListProperty(str)
    date = db.DateTimeProperty (required = True, auto_now = True, auto_now_add = True)

在我的代码的这一行:

 contract.parties = old_parties.append([str(person_id), person_condition])

从这个区块:

        (...)
        party = []
        contract_record = Contract(book_number = int(numBook),
                                   initial_page = int(numInitialPage),
                                   final_page = int(numFinalPage),
                                   contract_type = choosed_contract_type,
                                   parties = party)
        contract_record.put()
        contract_key = contract_record.key()
        contract_id = contract_record.key().id()

        submit_button = self.request.get('submit_button')

        if submit_button:
            if (person_name and valid_name(person_name)) and (user_SSN and valid_SSN(user_SSN)):
                person_record = Person(person_name = person_name,
                nacionality = user_nacionality,
                profession = user_profession,
                marital_status = user_marital_status,
                driver_license = int(user_driver_license),
                SSN = int(user_SSN),
                address = address)
                person_record.put()
                person_key = person_record.key()
                person_id = person_record.key().id()

                contract = Contract.get_by_id(contract_id)
                old_parties = contract.parties
                contract.parties = old_parties.append([str(person_id), person_condition])
                contract.put()

我收到了这个错误:

 File "C:\Users\Py\Desktop\contract\main.py", line 351, in post
    contract.parties = old_parties.append([str(person_id), person_condition])
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\db\__init__.py", line 614, in __set__
    value = self.validate(value)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\db\__init__.py", line 3435, in validate
    value = super(ListProperty, self).validate(value)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\db\__init__.py", line 641, in validate
    raise BadValueError('Property %s is required' % self.name)
BadValueError: Property parties is required

我的目标是:在contract属性中创建一个空列表的对象parties。之后,我想以这种方式将新列表[person_id, person_condition]添加到该空列表中:[ [person_id, person_condition], [person_id2, person_condition2],...]

我想知道是什么导致了这个错误以及如何修复它以实现我的目标。

感谢您的帮助!

1 个答案:

答案 0 :(得分:4)

我不确定这是否是导致您看到的GAE引擎的原因,但是append会在列表中附加并返回None。所以你想做contract.parties.append([str(person_id), person_condition])。你采用这种方式,最终将contract.parties设置为无。