query = leancloud.Query('Post')

这里 Query 的参数不对(不应该是字符串,应该是 leancloud.Object),
查询的写法请参考文档:https://leancloud.cn/docs/leanstorage_guide-python.html#hash-2027098679

您好,谢谢回复,已改用REST-API查询,还有个问题:云函数如何获取get请求的参数呢

已找到答案

query = leancloud.Query('Post')
post = query.get('5cc2c62743e78c9ba41c6e8f')
content = post.get('content')
return content

这样是可以查的吧,但我直接return query.find()为什么就报错呢

云引擎报这个错,哪位指导下
Traceback (most recent call last):
STDERR web1 19:51:40
File "/usr/share/pyenv/versions/3.7.3/lib/python3.7/site-packages/leancloud/engine/leanengine.py", line 146, in dispatch_request
STDERR web1 19:51:40
return Response(json.dumps(result), mimetype='application/json')
STDERR web1 19:51:40
File "/usr/share/pyenv/versions/3.7.3/lib/python3.7/json/__init__.py", line 231, in dumps
STDERR web1 19:51:40
return defaultencoder.encode(obj)
STDERR web1 19:51:40
File "/usr/share/pyenv/versions/3.7.3/lib/python3.7/json/encoder.py", line 199, in encode
STDERR web1 19:51:40
chunks = self.iterencode(o, oneshot=True)
STDERR web1 19:51:40
File "/usr/share/pyenv/versions/3.7.3/lib/python3.7/json/encoder.py", line 257, in iterencode
STDERR web1 19:51:40
return _iterencode(o, 0)
STDERR web1 19:51:40
File "/usr/share/pyenv/versions/3.7.3/lib/python3.7/json/encoder.py", line 179, in default
STDERR web1 19:51:40
raise TypeError(f'Object of type {o.class.name} '
STDERR web1 19:51:40
TypeError: Object of type Post is not JSON serializable

来个技术人员支持下....

发现了Object.dump(),但是没有[Object]转[]的方法啊,难道要进去再转,关键是里面还嵌套Object啊,希望官方可以给个解决方案呢

query = leancloud.Query('Post')
post = query.get('5cc2c62743e78c9ba41c6e8f')
content = post.get('content')return content

这样是可以查的吧

这样不可以查,之前回帖已经说了 leancloud.Query('Post') 里 Query 的参数不对(不应该是字符串,应该是 leancloud.Object),也给出了文档:https://leancloud.cn/docs/leanstorage_guide-python.html#hash-20270986794

发现了Object.dump(),但是没有[Object]转[]的方法啊,难道要进去再转

是的,需要自行转换,类似这样:

todo_list = leancloud.Query(leancloud.Object.extend('Todo')).descending('createdAt').find()
todo_list_json = json.dumps([todo.dump() for todo in todo_list])

里面还嵌套Object

里面还嵌套 Object 是指什么?

就是todo有个属性是Pointer类型的

嗯,dump 目前不支持 Pointer 类型,需要自行实现。

另外之前我搞错了,leadcloud.Query 同样接受 class 名称字符串(SDK 会自动转换)。
之前的问题在于 query.get(objectID) 返回的已经是 Object 了,而不是 Query,所以不需要再加 query.find(),而 query = leancloud.Query('Post'); query.find() 则返回一个 Object 列表。

要是这样的话还不如用REST-API了,另外:
currentUsername = leancloud.User.get_current().get_username()
STDERR web1 16:39:34
AttributeError: 'NoneType' object has no attribute 'get_username'
这个报错是什么原因,我要获取当前的用户

知道了,是因为header中没有加session,加了之后就好了