为什么我在mongoalchemy中遇到ExtraValueException?
这是我的类:
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命令行中使用它时,一切看起来都很好:
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
2 个回答
2
如果你遇到了 ExtraValueException 这个错误,可能是因为在类的定义里多加了一个逗号。比如说,如果你写成了:
...
pages = db.DocumentField(Page, required=False),
tags = db.StringField(max_length=120, required=False)
...
那么尝试使用 pages 或 tags 时就会出现 ExtraValueException 的错误。
这是我亲身经历过的教训。
1
我看了这个异常的文档说明,发现对于mongoalchemy来说,定义的模型并没有把当前页面(currentPage)注册为Presentation
文档的一个属性。不过在你复制的代码中,这个类定义里是有这个属性的。
如果你复制的这个类是你自己在项目中定义的,试着删除项目里的.pyc文件,然后重新运行应用程序。
顺便提一下,currentPage
这个变量名不符合PEP8命名规范。