AE native datastore accepts strings (encoded as ascii) and unicode as
key names in entities, but if you try to insert an object with an
unicode key using the mongodb backend (I didn't tried the other
backends) it fails.
The error comes from lines 349 - 350 in mongodb/datastore_mongo_db.py,
as it adds to the response pb the id received from saving the document
in mongodb:
id = self.__db[collection].save(document)
put_response.key_list().append(self.__key_for_id(id)._ToPb())
I guess it assumes the id is the id that we constructed previously in
the document. Mongodb always answers using utf-8, so the answer needs
to be converted to unicode
id = self.__db[collection].save(document).decode("utf-8")
put_response.key_list().append(self.__key_for_id(id)._ToPb())
another solution will be (I guess) to reuse the _id we put in the document.
I added a unit test for this case:
def testGetPutUnicode(self):
"""Sets and Gets models with unicode key names."""
class Author(db.Model):
name = db.StringProperty()
Author(name=u'Jes\u00fas Cebri\u00e1n',
key_name=u'jes\u00fascebri\u00e1n').put()
a = Author.get_by_key_name(u'jes\u00fascebri\u00e1n')
self.assertEqual('Author', a.kind())
self.assertEqual(u'Jes\u00fas Cebri\u00e1n', a.name)
a.delete()
del a
I'm quite new to mercurial, Tobias please can you instruct meon how do
you prefer to receive patches?
Thanks!
--
Joaquin Cuenca Abela