0
我试图用Google Cloud ndb和REST体系结构创建CRUD应用程序。JSON编码带密钥的NDB实体
因此,我有不同的API调用来创建和检索。
现在为了更新我需要在前端显示它们的实体,并给出一个标识符,所以ndb知道稍后要更新哪个实体。
我尝试用model.query
得到实体,然后将它们编码为JSON具有扩展编码序列化datetime
和ndb.Key
:
# JSONEncoder extension to handle datetime & ndb.Key
class CustomJsonEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime):
return obj.strftime('%Y-%m-%d-%H-%M-%S')
if isinstance(obj, ndb.Key):
return obj.urlsafe()
return json.JSONEncoder.default(self, obj)
# Get JSON data from ndb
query = Card.query(Card.category==category, ancestor=ancestor_key()).fetch()
cards = json.dumps([c.to_dict() for c in query], cls=CustomJsonEncoder)
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(cards)
我现在的问题是实体键就会消失,并且不显示在json.dump
了。我没有得到任何错误,它只是没有编码和传递。
打印时datetime
对象显示正确。关于如何发送URL安全ndb密钥以及json.dump
的任何想法?