我正在用flask和mongodb(mongoengine drive)开发一个项目。应用程序使用Note模型作为用户模式的嵌入文档。在
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
^{pr2}$回溯
mongoengine.errors.OperationError
OperationError: Update failed (cannot use the part (notes of notes.URLLink) to traverse the element ({notes: [ { _id: ObjectId('57d27bb24d2e9b04e175c0e5'), created_at: new Date(1473422818025), URLLink: "", content: "{
"_id" : ObjectId("57d27b414d2e9b04d79883b3"),
"created_at" : ISODate("2016-09-09T12:05:05.164Z"),
"URLLink" : "",
"content" : "c...", tags: [ "asd" ], isSecret: false, isArchived: false } ]}))
Traceback (most recent call last)
File "/Users/ozer/Documents/localhost/copylighter/env/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/ozer/Documents/localhost/copylighter/env/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Users/ozer/Documents/localhost/copylighter/env/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/ozer/Documents/localhost/copylighter/env/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/Users/ozer/Documents/localhost/copylighter/env/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/ozer/Documents/localhost/copylighter/env/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/ozer/Documents/localhost/copylighter/env/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/ozer/Documents/localhost/copylighter/env/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/ozer/Documents/localhost/copylighter/env/lib/python2.7/site-packages/flask_login.py", line 792, in decorated_view
return func(*args, **kwargs)
File "/Users/ozer/Google Drive/localhost/copylighter/views.py", line 225, in update_quote
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)
#note.save()
File "/Users/ozer/Documents/localhost/copylighter/env/lib/python2.7/site-packages/mongoengine/document.py", line 468, in update
return self._qs.filter(**self._object_key).update_one(**kwargs)
File "/Users/ozer/Documents/localhost/copylighter/env/lib/python2.7/site-packages/mongoengine/queryset/base.py", line 490, in update_one
upsert=upsert, multi=False, write_concern=write_concern, **update)
File "/Users/ozer/Documents/localhost/copylighter/env/lib/python2.7/site-packages/mongoengine/queryset/base.py", line 472, in update
raise OperationError(u'Update failed (%s)' % unicode(err))
OperationError: Update failed (cannot use the part (notes of notes.URLLink) to traverse the element ({notes: [ { _id: ObjectId('57d27bb24d2e9b04e175c0e5'), created_at: new Date(1473422818025), URLLink: "", content: "{ "_id" : ObjectId("57d27b414d2e9b04d79883b3"), "created_at" : ISODate("2016-09-09T12:05:05.164Z"), "URLLink" : "", "content" : "c...", tags: [ "asd" ], isSecret: false, isArchived: false } ]}))
我想这是我写在下面的重要部分。我没有找到任何关于它的查询集,关于mongoengine的文档也很少。我在哪里找?在
current_user.update(notes__tags=tagList, notes__content=form.content2.data, notes__URLLink=form.URLLink2.data)
这里有几个问题:
Note
类需要继承EmbeddedDocumentUser
对象使用更新操作考虑下面的示例,该示例将向用户注释字段添加注释。在
您可能还想阅读this answer关于何时使用
EmbeddedDocument
与ReferenceField
的比较。在相关问题 更多 >
编程相关推荐