Vas*_*tos 2 python string mongodb pymongo flask
我正在使用 Flask 和 pymongo 开发一个项目,我有一个用户集合和一个电影集合,其中实例如下:
user = {"Email":"user@gmail.com" , "Comments":[" Hobbit 2013 :good movie" , " Hobbit 2013 :bad movie" , " Spiderman 2020 :very good"]}
Run Code Online (Sandbox Code Playgroud)
我还有一个电影集合,其中电影实例如下:
movie = {"title":"Hobbit" , "year":"2013" , "comments":["Hobbit 2013 user@gmail.com:good movie"] }
Run Code Online (Sandbox Code Playgroud)
我正在使用 jinja 2 模板向特定电影提交新标题,如果有任何用户评论了该电影,我希望每个评论过特定电影的用户在其评论中都包含新电影名称。对于上述用户实例,霍比特人 -> 复仇者联盟
`user = {"Email":"user@gmail.com" , "Comments":[" Avengers 2013 :good movie" , " Avengers 2013 :bad movie" , " Spiderman 2020 :very good"]}`
`movie = {"title":"Avengers" , "year":"2013" , "comments":["Avengers 2013 user@gmail.com:good movie"] }` #obviously a movie can have more comments from users not just one
Run Code Online (Sandbox Code Playgroud)
使用烧瓶端点,我成功地更改了电影标题,但是我无法更改旧电影标题所在的任何用户评论,因为标题不会与我的代码发生变化
这是我的终点。我从 jinja2 表单中获取新标题,然后尝试用新标题替换注释中的旧标题。然后我得到错误
pymongo.errors.InvalidOperation: cannot set options after executing query
Run Code Online (Sandbox Code Playgroud)
我的代码:
@app.route('/executemovieupdate' , methods = ['GET', 'POST'])
def execute_movie_update():
if 'Email' in session and 'User' in session:
email = session['Email']
user = session['User']
if user == 'Admin':
if 'Movie' in session and 'Movie_year' in session and 'Movie_plot' in session:
movie = session['Movie']
old_movie = str(movie) #store old movie title
print("old movie is " , old_movie)
year = session['Movie_year']
plot = session['Movie_plot']
tainia = movies.find_one({'title':movie , "year":year})
if request.method == 'POST':
new_title = request.form.get('new-title') #get the new title from jinja2 template
if new_title:
print("update the title")
movies.update_one({"title":movie , "year":year} , {"$set":{"title":new_title} } ) #the title is updated succesfully for the movie
session['Movie'] = new_title
movie = session['Movie']
user_list = users.find({})
for idxu, usr in enumerate(user_list): #this is where i try to change the title in the user comments
for idxc, comment in enumerate(usr['Comments']):
if old_movie in comment:
print("old comment here")
print(comment)
user_list[idxu]['Comments'][idxc] = comment.replace(old_movie ,new_title) #this is the line where the error occurs
print(comment) # the new comment is printed
return ''' movie has been updated '''
else:
return render_template('movie-update.html' , movie = tainia)
else:
return redirect(url_for('admin.html'))
else:
return redirect(url_for('login'))
else:
return redirect(url_for('login'))
Run Code Online (Sandbox Code Playgroud)
我非常感谢您对这个问题的帮助。先感谢您。
如果您打印,type(user_list)您会注意到它不是一个列表,dict而是一个pymongo.cursor.Cursor对象(只读),因此您可以更改user_list = users.find({})为user_list = list(users.find({}))(但要小心,它会查询 collection 中的所有记录users)并且它应该可以工作。
另一种更好的方法是使用usr['Comments'][idxc]( 这是 a dict) 而不是user_list[idxu]['Comments'][idxc]
如果你想将该记录更新到 MongoDB 中,那么你将需要使用update_one上面类似的方法
我没有你的数据,但类似这样的事情应该可以解决问题:
for idxu, usr in enumerate(user_list): #this is where i try to change the title in the user comments
for idxc, comment in enumerate(usr['Comments']):
if old_movie in comment:
print("old comment here")
print(comment)
usr['Comments'][idxc] = comment.replace(old_movie ,new_title) #this is the line where the error occurs
print(comment) # the new comment is printed
users.update_one({"_id": usr['_id']}, {"$set": {"Comments": usr['Comments']}})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8090 次 |
| 最近记录: |