【问题标题】:Why am I getting an ExtraValueException from mongoalchemy?为什么我会从 mongoalchemy 获得 ExtraValueException?
【发布时间】:2011-12-09 18:15:26
【问题描述】:

这是我的课:

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

【问题讨论】:

    标签: python mongodb sqlalchemy flask


    【解决方案1】:

    对于出现 ExtraValueException 的任何其他人,如果您在 Class 定义中添加了一个额外的逗号,您也会看到此错误。例如,如果您有:

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

    尝试使用页面或标签都会产生 ExtraValueException。

    这是我从自己的痛苦中学到的。

    【讨论】:

      【解决方案2】:

      我阅读了doc string of the exception,似乎对于 mongoalchemy,定义的模型没有将 currentPage 注册为 Presentation 文档的属性,但是在您复制粘贴的代码中,定义了类定义的属性。

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

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

      【讨论】:

      • 谢谢。有些东西不同步。我最终按照您的建议删除了 pyc 文件,并通过 db.collection.remove() 和 .drop() 清理了文档。这似乎已经解决了它。也感谢您指出命名约定
      • 当你尝试修复某些东西时,你最好一步一步地去做,比如:1)删除pyc 2)测试它是否没有修复尝试其他的东西,比如清理数据库。这样您就可以更好地了解您使用的工具,并且可以防止将来出现错误。
      猜你喜欢
      • 2019-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-30
      • 2019-12-16
      • 2011-01-21
      • 1970-01-01
      相关资源
      最近更新 更多