开发者

How to query filtering by a ByteString field in Google App Engine?

I have

class Map(db.Model):
    urlHash= db.ByteStringProperty()

hasher = hashlib.sha256()
hasher.update(staticMapUrl)
urlHash = hasher.digest()
query = db.Query(models.Map)
query = query.filter('urlHash =', urlHash)
results = query.fetch(1) 

and this type of query tries to decode the urlHash to a string, throwing an exception

UnicodeDecodeError: 'ascii' codec can'开发者_高级运维t decode byte 0xfe in position 0: ordinal not in range(128)


Looks like it will work if you explicitly make the hash into a ByteString:

from google.appengine.api.datastore_types import ByteString

hasher = hashlib.sha256()
hasher.update('http://www.google.com/')
urlHash = hasher.digest()
bs = ByteString(urlHash)

m = Map(urlHash=bs).put()

query = db.Query(Map)
query = query.filter('urlHash =', bs)
results = query.fetch(1)


One solution I found is to encode manually to base64

urlHash = hasher.digest().encode('base64')

I noted that besides the name of the exception being UnicodeDecodeError, it occurs on encoding too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜