我正在使用django-mongodb-engine为android应用程序编写一个基于django的后端,并尝试从PUT请求中获取数据以更新数据库中的记录。我正在获取一个用户名,并在数据库中过滤具有该用户名的user对象(成功),但save函数似乎没有保存更改。我可以断定更改没有被保存,因为当我进入mLab的在线数据库管理工具时,更改并不在那里。
代码如下:
existing_user = User.objects.filter(userName = user_name)
if existing_user == None:
response_string += "<error>User not identified: </error>"
elif (existing_user[0].password != user_pwd):
response_string += "<error>Password error.</error>"
#if we have a validated user, then manipulate user data
else:
existing_user[0].star_list.append(new_star)
existing_user[0].save()我没有收到任何错误消息,但数据保持不变。完成上述操作后,star_list将保持为空。实际上,作为测试,我甚至尝试将上面的else子句替换为:
else:
existing_user[0].userName = "Barney"
existing_user[0].save()这个调用之后,existing_user.userName仍然是它的原始值("Fred",而不是"Barney")!
发布于 2016-08-14 09:31:34
找到了这个问题的答案。我不确定为什么它没有像list那样工作,但试图通过列表访问对象时出现了问题……换句话说,以下代码不起作用:
existing_user[0].userName = "Barney"
existing_user[0].save()但这确实做到了:
found_user = existing_user[0]
found_user.userName = "Barney"
found_user.save()根据我对Python列表的理解,这两个列表应该是相同的……但是也许一些python天才可以解释为什么它们不是呢?
https://stackoverflow.com/questions/38929708
复制相似问题