hmarr/mongoengine — Issue #409: id evaluated every time a document class is instantiated
Author: maxcountryman
Created: 2012-01-15 03:33:47+00:00
State: open

Hi,

I have an id I'd like to automatically increment. I did this by creating a Counter document which simply has a next field which is incremented by a utility function. In order to set this on a document I set my id field as such:

id = db.StringField(default=lambda: increment('document_counter'), primary_key=True)

This works as you would expect, except for one slight problem: every time the document is called, the Counter document instance's next attribute is incremented! I was under the impression that the id field wouldn't be evaluated until the document was actually saved.

Is there some kind of workaround for this?