Why am I getting an ExtraValueException from mongoalchemy?

Question:

Here is my class:

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)

When I use it from the mongo shell, everything seems fine:

db.Presentation.find({id:2})

{ "_id" : ObjectId("4e9cdddd0ad5c97ee6000000"), 
"author" : "admin", "currentPage" : 3, "id" : 2, 
"pages" : { "content" : "", "pagenum" : 0 }, "title" : "dd" }

but when I am using 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
Asked By: Siddharth Ram

||

Answers:

I read the doc string of the exception and It seems like for mongoalchemy the model defined doesn’t register currentPage as an attribute of Presentation document, but in the code you copy pasted the class definition define the attribute.

If the class you copy pasted is the class that you defined in your project, try to delete .pyc files in your project and re-run the application.

By the way currentPage variable name does not follow PEP8 Naming Conventions.

Answered By: amirouche

For anyone else with a ExtraValueException, you can also see this error if you put an extra comma in the Class definition. For example if you had had:

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

trying to use either pages or tags would give an ExtraValueException.

This I learnt to my own pain.

Answered By: Col Wilson
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.