I have model:
from flask.ext.security import currennt_user
#instance of LocalProxy wrapped model User(db.Document)
class ContactModel(db.Document, SomeMixin):
user = db.ReferenceField(User, verbose_name='User', required=True)
And faced strange behavior of ReferenceField. Why is working following code:
model = ContactModel(user = current_user.pk, ....)
And does not following:
model = ContactModel()
model.user = current_user.pk
The same issue when I trying to do: model = ContactModel() model.user = current_user
Last two pieaces of code throws an error: ValidationError: ValidationError (ContactModel:None) (A ReferenceField only accepts DBRef or documents: ['user'])
current_user.pk
hasObjectId
type (just id, no info about collection).current_user
hasLocalProxy
type.You can't save reference as
ObjectId
because there are no information about reference collection and mongo use for thisBDRef
.You can get
DBRef
object from mongoengine document withDocument.to_dbref
method.So mognoenginge check type to get
DBRef
explicitly or get it from document withto_dbref
.For
current_user
you can callto_dbref
to getDBRef
object or_get_current_object()
to get realUser
object.