【发布时间】:2021-02-20 12:21:31
【问题描述】:
我有一个User 类,其结构如下:
class User(UserMixin, db.Entity):
id = PrimaryKey(int, auto=True)
vorname = Required(str)
nachname = Required(str)
email = Required(str)
password_hash = Required(str)
role = Required(str)
def set_password_hash(self, password):
self.password_hash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.password_hash, password)
不幸的是,创建一个用户对象然后自动存储在数据库中是不可能的,因为我只从网络表单中获取原始的password 字段值。
with db_session:
new_user = User(
vorname=form.vorname.data,
nachname=form.nachname.data,
email=form.email.data,
role=form.role.data,
)
new_user.set_password_hash(form.password.data) # this line is not called, since the creation of new_user fails because of the missing password field.
是否可以推迟保存到数据库?还是使用我的自定义函数来设置password_hash 值?
【问题讨论】: