如何在 flask-admin 中呈现给用户的编辑表单之前访问模型字段值?
How to access model field value before edit form presented to user in flask-admin?
我有模型用户:
class User(db.Model):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
username = db.Column(db.String(50), unique=True, nullable=False)
token_life_time = db.Column(db.Integer, nullable=False)
和来自 flask-admin 的 ModelView:
class UserModelView(ModelView):
column_list = ('id', 'username', 'token_life_time')
form_create_rules = ('username', 'token_life_span') # custom field, that doesn't exist in actual model
form_edit_rules = ('username', 'token_life_time', 'new_token_life_time')
form_extra_fields = {
'token_life_span': StringField(default='1-0-0-0', validators=[DataRequired()]),
'new_token_life_time': StringField() # how to put here actual value from model.token_life_time as default value in EDIT form ???
}
column_formatters = dict(
token_life_time=lambda v, c, m, p: UserModelView.convert_seconds_to_human_readable(m.token_life_time)
)
def on_model_change(self, form, model, is_created):
token_time = form.data.get('token_life_span', None)
if token_time:
model.token_life_time = self.convert_to_seconds(token_time) # my custom function that converts string of format (days-hours-minutes-seconds, example: 1-8-23-15) into seconds for storing in DB int type
我想在 EDIT 表单呈现给用户之前访问 model.token_life_time 值并将其从秒数转换为人类可读的字符串(包括天数、小时数等)。然后将转换后的值作为默认值放入编辑表单的自定义字段中(new_token_life_time)。
问题是 - 如何从模型中访问实际值,然后将转换后的值作为默认值以编辑形式放入自定义字段???
覆盖视图的 edit_form
方法 - 请参阅 documentation。
类似于以下内容:
class UserModelView(ModelView):
def edit_form(self, obj=None):
form = super().edit_form(obj=obj)
# calculate token_life_span
_token_life_span = get_token_life_span(obj.token_life_time)
form.token_life_span.data = _token_life_span
return form
我有模型用户:
class User(db.Model):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
username = db.Column(db.String(50), unique=True, nullable=False)
token_life_time = db.Column(db.Integer, nullable=False)
和来自 flask-admin 的 ModelView:
class UserModelView(ModelView):
column_list = ('id', 'username', 'token_life_time')
form_create_rules = ('username', 'token_life_span') # custom field, that doesn't exist in actual model
form_edit_rules = ('username', 'token_life_time', 'new_token_life_time')
form_extra_fields = {
'token_life_span': StringField(default='1-0-0-0', validators=[DataRequired()]),
'new_token_life_time': StringField() # how to put here actual value from model.token_life_time as default value in EDIT form ???
}
column_formatters = dict(
token_life_time=lambda v, c, m, p: UserModelView.convert_seconds_to_human_readable(m.token_life_time)
)
def on_model_change(self, form, model, is_created):
token_time = form.data.get('token_life_span', None)
if token_time:
model.token_life_time = self.convert_to_seconds(token_time) # my custom function that converts string of format (days-hours-minutes-seconds, example: 1-8-23-15) into seconds for storing in DB int type
我想在 EDIT 表单呈现给用户之前访问 model.token_life_time 值并将其从秒数转换为人类可读的字符串(包括天数、小时数等)。然后将转换后的值作为默认值放入编辑表单的自定义字段中(new_token_life_time)。
问题是 - 如何从模型中访问实际值,然后将转换后的值作为默认值以编辑形式放入自定义字段???
覆盖视图的 edit_form
方法 - 请参阅 documentation。
类似于以下内容:
class UserModelView(ModelView):
def edit_form(self, obj=None):
form = super().edit_form(obj=obj)
# calculate token_life_span
_token_life_span = get_token_life_span(obj.token_life_time)
form.token_life_span.data = _token_life_span
return form