【发布时间】:2019-01-20 02:57:28
【问题描述】:
我有一个非常有趣的问题(对我来说完全是),我有查询,在这个查询中我有列表行:
class TestList(ndb.Model):
testing_list = ndb.StringProperty(repeated=True)
list_id = ndb.IntegerProperty()
我还有通过 PATCH 请求更改 testing_list 的 API 方法。 代码:
@app.route('/list/change', methods=['PATCH'])
def list_change():
list_id = request.form['id']
list_elements = request.form['elements']
query = TestList.query(TestList.list_id == int(list_id))
try:
query.fetch()[0].list_id
except IndexError:
return 'Error', 400
new_elements = str(list_elements).replace(' ', '').split(',')
query.fetch()[0].testing_list = [element for element in new_elements if element in query.fetch()[0].testing_list]
query.fetch()[0].put()
testing_list_extend(query.get(), new_elements)
return 'Success', 200
@ndb.transactional
def testing_list_extend(list_key, new_elements):
for element in new_elements:
query = TestList.query(ancestor=list_key.key)
if element not in query.fetch()[0].testing_list:
query.fetch()[0].testing_list.append(element)
query.fetch()[0].put()
return '200'
在输入时,我得到类似'Element1,Element2'的字符串,这是正文请求中的elements和id,如'1'。所以在我解析字符串并制作列表之后。在我想在 testing_list 中添加新的独特元素之后。在这部分我有错误:有时,当我添加新元素并通过 GET 请求获取 testing_list 时,我得到空列表,但在 15-30 秒内我得到列表前段时间就想看 例如在正文 PATCH 请求中:
id = '1'
elements = 'Element1, Element2'
我通过获得 testing_list 等待什么响应:
[Element1, Element2]
我经常得到的:
[Element1, Element2]
我得到的东西非常罕见(我认为是错误):
[]
【问题讨论】:
标签: python-2.7 google-app-engine google-cloud-datastore app-engine-ndb