所以我有这个 mongodb 文档(见下文) 我正在使用这个 donateList和requestList更新我的 mongodb 文档。所以情况是这样的:如果我的 web 应用程序中的用户捐赠了一本书。我想通过更新文档来保存那本书的bookid 。但是因为每次我无法保存以前的 bookid时它都会更新文档。与requestList案例相同。我想要的只是每次用户捐赠或请求一本书时保存bookid 。
代码
@app.route('/donate', methods=['GET', 'POST'])
@app.route('/donate books', methods=['GET', 'POST'])
def donate_books():
form = DonateBooks()
if 'email' in session and request.method == 'GET':
return render_template('donate.html', title="Donate Books", form=form)
elif 'email' in session and request.method == 'POST':
if form.validate() == False:
flash('All fields are required !')
return render_template('donate.html', title="Donate books", form=form)
else:
donate = mongo.db.mylogin
Donating_books = donate.insert(
{'bookname': form.bookname.data, 'bookdesc': form.description.data, 'donatedby': session['username']})
flash('Successfully donated !', 'success')
bookId = donate.find_one({"bookname" : form.bookname.data })
final_id = dict(bookId)
new_id = final_id['_id']
inserting_id = donate.update_one({'name' : session['username']}, {'$set': {"donateList": [{"bookid": new_id}], "requestList" : [{"bookid" : new_id}]}})
return render_template('donate.html', title='Donate Books', form=form)
else:
flash('Please Login/Sign Up first to Donate Books !')
return redirect(url_for('home'))