这是我的班级:
class Presentation(db.Document):
title = db.StringField(max_length=120, required=True)
author = db.StringField (required=True)
pages = db.DocumentField(Page, required=False)
tags = db.StringField(max_length=120, required=False)
id = db.IntField(required=True)
currentPage = db.IntField()
def __str__(self):
return 'Title:%s author:%s id:%d currentPage:%d' % ( self.title, self.author,self.id,self.currentPage)
当我从mongo shell中使用它时,一切似乎都很好:
db.Presentation.find({ID:2})
{ "_id" : ObjectId("4e9cdddd0ad5c97ee6000000"),
"author" : "admin", "currentPage" : 3, "id" : 2,
"pages" : { "content" : "", "pagenum" : 0 }, "title" : "dd" }
但是当我使用MongoAlchemy时,
p = query.filter(Presentation.id == 2).first()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.macosx-10.7-intel/egg/mongoalchemy/query.py", line 136, in first
File "build/bdist.macosx-10.7-intel/egg/mongoalchemy/query.py", line 388, in next
File "build/bdist.macosx-10.7-intel/egg/mongoalchemy/document.py", line 318, in unwrap
File "build/bdist.macosx-10.7-intel/egg/mongoalchemy/document.py", line 152, in __init__
mongoalchemy.exceptions.ExtraValueException: currentPage
答案 0 :(得分:2)
对于具有ExtraValueException的任何其他人,如果在类定义中添加额外的逗号,也会看到此错误。例如,如果你有:
...
pages = db.DocumentField(Page, required=False),
tags = db.StringField(max_length=120, required=False)
...
尝试使用页面或标签会产生ExtraValueException。
这让我了解了自己的痛苦。
答案 1 :(得分:1)
我读了doc string of the exception并且似乎对于mongoalchemy而言,定义的模型没有将currentPage注册为Presentation
文档的属性,但是在您复制粘贴的代码中,类定义定义了属性。
如果您复制粘贴的类是您在项目中定义的类,请尝试删除项目中的.pyc文件并重新运行该应用程序。
顺便提一句currentPage
变量名称不符合PEP8命名约定。