2011-03-08 86 views
1

我有如何通过Google App Engine中的ByteString字段查询过滤?

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) 

和这种类型的查询试图将urlHash解码为字符串,抛出异常

UnicodeDecodeError: 'ascii' codec can't decode byte 0xfe in position 0: ordinal not in range(128)

+0

PLS显示设置urlHash用于查询的代码。 – cope360 2011-03-08 20:18:05

+0

@ cope360我将代码添加到问题主体 – 2011-03-08 21:31:27

+0

您可以包含例外的完整堆栈跟踪吗? – 2011-03-09 00:22:34

回答

1

看起来它会工作,如果你明确地使散列成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) 
+0

我现在无法测试它,但它似乎是合理的 – 2011-03-09 14:19:46

0

一种解决方案,我发现是手动编码为Base64

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

我注意到,除了例外的名称是UnicodeDecodeError,它也发生在编码上。

相关问题