2011-10-18 85 views
3

这里是我的课:为什么我从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) 

当我使用它从蒙戈外壳,一切似乎罚款:

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)。首先()

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 

回答

1

读取doc string of the exception和它似乎是mongoalchemy模型中定义没有按注册currentPage作为Presentation文档的一个属性,但是在复制的代码中粘贴的类定义定义了属性。

如果粘贴的类是您在项目中定义的类,请尝试删除项目中的.pyc文件并重新运行该应用程序。

顺便说一下currentPage变量名不遵循PEP8命名约定。

+0

谢谢。有些东西不同步。我最终删除了像您建议的pyc文件,并通过db.collection.remove()和 .drop()清除了文档。这似乎修复了它。还要感谢您在尝试解决某些问题时指出命名约定 –

+0

,您最好一步一步来做,例如:1)移除pyc 2)测试它是否不固定尝试其他方法,如清理数据库。通过这种方式,您可以更好地了解您使用的工具并防止将来发生错误。 – amirouche

2

对于任何具有ExtraValueException的人,如果您在类定义中添加额外的逗号,也可以看到此错误。例如,如果您曾有:

... 
pages = db.DocumentField(Page, required=False), 
tags = db.StringField(max_length=120, required=False) 
... 

尝试使用页面或标记会导致ExtraValueException。

这让我学到了自己的痛苦。

相关问题