在 Mongoengine 0.7.10 中,我仍然可以执行以下操作:
class User(db.Document):
email = db.EmailField(unique=True, required=True)
_password = db.StringField(max_length=255, required=True)
@property
def password(self):
return self._password
@password.setter
def password(self, password):
self._password = bcrypt.generate_password_hash(password)
user = User(email='1@1.com', password='12345')
但是,上面的代码在 0.8.0 中中断:
ValidationError: ValidationError (User:None) (_password.Field is required: ['User'])
似乎 MongoEngine 在启动期间无法识别我的自定义密码设置器。我必须手动编写这些来修复它:
user = User(email='1@1.com')
user.password='12345'
这可能是由于以下更改(来自Mongonengine 0.8 升级说明):
Previously, if you had data the database that wasn’t defined in the Document definition, it would set it as an attribute on the document. This is no longer the case and the data is set only in the document._data dictionary:
我想知道这是有意的还是MongoEngine中的错误?在我的模型中编写自定义属性设置器的最佳实践是什么?