Google App Engine。蟒蛇。 NDB。 BadValueError:预期的字符串,得到<type'str'=“”>

时间:2015-11-17 22:04:27

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

我一直试图理解这个错误一段时间,但找不到答案。

我有一个模型 - 地址 - 我正在数据存储区中创建一个新实体。

这是模型:

class Address(ndb.Model):
    """
    model for storing the addresses of the club, both physical and for mailing
    """
    club_key = ndb.KeyProperty(kind="Club")
    address_type = ndb.StringProperty()  #: physical, mailing
    street = ndb.StringProperty()  #: or PO Box if necessary
    city = ndb.StringProperty()
    province = ndb.StringProperty()
    region = ndb.StringProperty()
    post_code = ndb.StringProperty()
    country = ndb.StringProperty()
    coordinates = ndb.GeoPtProperty()  #: for physical address
    date_added = ndb.DateTimeProperty(auto_now_add=True)

    #: method to save a new address
    @classmethod
    def save_address(cls, club_key, address_type, street, city, province, region, post_code, 
        country, coordinates, id_string):
        new_address = cls(club_key=club_key, address_type=address_type, street=str, 
        city=city, province=province, region=region, post_code=post_code, country=country, 
        coordinates=coordinates,
                          id=id_string).put()
        return new_address.id()

以下是用于创建地址实体的处理程序:

class AddNewAddressHandler(BaseHandler):
"""
a handler to add the address info for a club
"""
def post(self):
    club_key = self.get_url_string(-1)
    club_key = ndb.Key(urlsafe=club_key)
    address_type = self.request.get("type")
    street = self.request.get("street")
    city = self.request.get("city")
    province = self.request.get("province")
    region = self.request.get("region")
    postcode = self.request.get("postcode")
    country = self.request.get("country")
    coordinates = self.request.get("coordinates")
    coordinates = ndb.GeoPt(coordinates)
    club = self.get_url_string(-2)
    club = unquote(club)  # from urllib2
    id_string = address_type + "-" + club
    id_string = id_string.strip().replace(" ","-").lower()
    db.Address.save_address(club_key, address_type, street, city, province, region, 
        postcode, country, coordinates, id_string)
    redirect_str = self.get_url_string(-3)
    self.redirect("admin/club_dashboard/%s" % redirect_str)

然后抛出以下错误:

Traceback (most recent call last):
  File "C:\Program Files\Google\google_appengine\lib\webapp2 2.5.2\webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
  File "C:\Program Files\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
  File "C:\Program Files\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
  File "C:\Program Files\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1102, in __call__
return handler.dispatch()
  File "C:\Program Files\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
  File "C:\Program Files\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
  File "C:\Users\Me\PycharmProjects\my_app\admin\admin.py", line 95, in post
id_string)
  File "C:\Users\Me\PycharmProjects\my_app\admin\database\models.py", line 62, in save_address
id=id_string).put()
  File "C:\Program Files\Google\google_appengine\google\appengine\ext\ndb\model.py", line 2927, in __init__
self._set_attributes(kwds)
  File "C:\Program Files\Google\google_appengine\google\appengine\ext\ndb\model.py", line 2973, in _set_attributes
prop._set_value(self, value)
  File "C:\Program Files\Google\google_appengine\google\appengine\ext\ndb\model.py", line 1128, in _set_value
value = self._do_validate(value)
  File "C:\Program Files\Google\google_appengine\google\appengine\ext\ndb\model.py", line 1075, in _do_validate
value = self._call_shallow_validation(value)
  File "C:\Program Files\Google\google_appengine\google\appengine\ext\ndb\model.py", line 1267, in _call_shallow_validation
return call(value)
  File "C:\Program Files\Google\google_appengine\google\appengine\ext\ndb\model.py", line 1314, in call
newvalue = method(self, value)
  File "C:\Program Files\Google\google_appengine\google\appengine\ext\ndb\model.py", line 1764, in _validate
(value,))
BadValueError: Expected string, got <type 'str'>

我确信这很明显,我只是看不到它。

3 个答案:

答案 0 :(得分:2)

在save_address()中,'street = str'应为'street = street'。

答案 1 :(得分:1)

我想你想要street=street

答案 2 :(得分:0)

在Python中编写string.method()时,方法的返回会自动传递给字符串,您不需要说newString = string.method()。实际上,string.method()不是为了返回任何东西,所以它返回&#34;类型&#34;变量而不是字符串的新值。

只需替换

id_string = id_string.strip().replace(" ","-").lower()

id_string.strip().replace(" ","-").lower()