Python 使用Flask更新文档中的嵌入文档-Mongoengine Mongoengine.errors.OperationError
我正在与flask和mongodb(mongoengine drive)一起开发一个项目。应用程序使用注释模型作为用户模式的嵌入文档Python 使用Flask更新文档中的嵌入文档-Mongoengine Mongoengine.errors.OperationError,python,mongodb,flask,mongoengine,Python,Mongodb,Flask,Mongoengine,我正在与flask和mongodb(mongoengine drive)一起开发一个项目。应用程序使用注释模型作为用户模式的嵌入文档 class User(db.Document, UserMixin): fields like created_at, content, slug etc... notes = db.ListField(db.EmbeddedDocumentField('Note')) class Note(db.Document): fields l
class User(db.Document, UserMixin):
fields like created_at, content, slug etc...
notes = db.ListField(db.EmbeddedDocumentField('Note'))
class Note(db.Document):
fields like created_at, content, slug, URLLink, isSecret etc...
content = db.StringField(required=True)
tags = db.ListField(db.StringField(required=True, max_length=20)
当我尝试更新便笺时,没关系,但是当我尝试在用户集合中附加更新的便笺时,我失败了
视图.py
@app.route("/update_quote/<string:id>" ,methods=['POST'])
@login_required
def update_quote(id):
note = Note.objects(id=id).first()
form = UpdateNoteForm()
if request.method == 'POST':
form = UpdateNoteForm(request.form)
if form.validate == False:
flash('Faliure','danger')
return redirect(url_for('profile')+('/'+current_user.slug))
if form.validate_on_submit():
tags = form.tags2.data
tagList = tags.split(",")
note = Note.objects.get(id=form.wtf.data)
note.update(content=form.content2.data, tags=tagList, URLLink=form.URLLink2.data)
current_user.notes.append(note)
current_user.update(notes__tags=tagList, notes__content=form.content2.data, notes__URLLink=form.URLLink2.data)
flash("Success","success")
return render_template("update.html", title="delete", form=form, note=note )
我想这是我写在下面的重要部分。我没有找到任何关于它的查询集,关于mongoengine的文档也很少。我在哪里找
current_user.update(notes__tags=tagList, notes__content=form.content2.data, notes__URLLink=form.URLLink2.data)
这里有几个问题:
- 您可以(在最新版本的mongoengine中)使用
类需要继承EmbeddedDocument注意
- 不能以您正在尝试的方式对
对象使用更新操作用户
import mongoengine as mdb
from numpy.random.mtrand import randint
mdb.connect("embed-test")
class User(mdb.Document):
name = mdb.StringField()
notes = mdb.EmbeddedDocumentListField('Note')
class Note(mdb.EmbeddedDocument):
content = mdb.StringField(required=True)
tags = mdb.ListField(mdb.StringField(required=True, max_length=20))
User.drop_collection()
u = User(name="Name").save()
if __name__ == "__main__":
new_note = Note(content="Some notes here.", tags=['one', 'two'])
current_user = User.objects.first()
current_user.notes.append(new_note)
current_user.save()
您可能还想了解何时使用
EmbeddedDocument
与使用ReferenceField
的对比 感谢@steve rossiter,我做了更改,但它仍然返回了相同的回溯错误。好的,您可以编辑您的答案以使用您所做的更改进行更新吗?很抱歉,我无法编辑我的答案。所以我在这里写作。当我检查我的代码以便为您复制它时,我意识到我没有将我的Note模态类更改为class Note(db.EmbeddedDocument):我做了这个更改,但现在我正在将Note插入shell。您不需要保存EmbeddedDocument,只需将其分配到EmbeddedDocumentField或将其附加到EmbeddedDocumentListField,我建议您阅读?您需要了解mongoengine如何从mongodb文档映射到python对象的基础知识。
import mongoengine as mdb
from numpy.random.mtrand import randint
mdb.connect("embed-test")
class User(mdb.Document):
name = mdb.StringField()
notes = mdb.EmbeddedDocumentListField('Note')
class Note(mdb.EmbeddedDocument):
content = mdb.StringField(required=True)
tags = mdb.ListField(mdb.StringField(required=True, max_length=20))
User.drop_collection()
u = User(name="Name").save()
if __name__ == "__main__":
new_note = Note(content="Some notes here.", tags=['one', 'two'])
current_user = User.objects.first()
current_user.notes.append(new_note)
current_user.save()