2011-10-18 82 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。

這讓我學到了自己的痛苦。

相關問題