我想构建一个表单来一次编辑具有多对一关系的多个实体。
要素工程模型具有许多相关的要素列。
这些要素列的内容是通过一个函数自动生成的(对于给定文件的每个列,用于要素工程)。
现在,我希望featureEngineeringDetailsForm列出featureColumn模型中的所有列,并且只有一个属性(使用标记)可编辑。我知道可以创建FeatureColumnForm的许多实例(一个字段),每个实例都有一个提交功能。
但是还有一种方法可以让动态范围的编辑字段全部提交一次,然后循环执行route.py?
感谢任何提示。
class FeatureEngineering(db.Model):
id = db.Column(db.Integer, primary_key=True)
project_id = db.Column(db.Integer, db.ForeignKey('project.id'))
train_file = db.Column(db.String(100))
test_file = db.Column(db.String(100))
description = db.Column(db.Text) # RB turn to function Text()?
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
columns = db.relationship('FeatureColumn', backref='columns',lazy='dynamic')
def __repr__(self):
return '<Feature Engineering {}>'.format(self.name)
class FeatureColumn(db.Model):
id = db.Column(db.Integer, primary_key=True)
feature_engineering_id = db.Column(db.Integer, db.ForeignKey('feature_engineering.id'))
sample = db.Column(db.String(500))
use = db.Column(db.Boolean)
val_type = db.Column(db.String(100))
val_count = db.Column(db.Integer)
val_unique_count = db.Column(db.Integer)
val_min = db.Column(db.Float)
val_max = db.Column(db.Float)
val_mean = db.Column(db.Float)
val_stddev = db.Column(db.Float)
val_skew = db.Column(db.Float)
val_kurtosis = db.Column(db.Float)
guess = db.Column(db.String(140))
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
def __repr__(self):
return '<Column {}>'.format(self.id)
修改
嗨,如果有人正在寻找解决方法,请在具有多个提交按钮的实现下方。
我首先在routes.py中构建表单的字典,然后在模板中循环遍历。 为了了解已提交的表单,我添加了一个动态隐藏字段并将其值设置为column_id,因此在捕获POST时,可以识别出提交表单的来源。
forms.py:
class FeatureColumnForm(FlaskForm):
column_id = HiddenField('column_id')
action = StringField('Action', validators=[DataRequired()])
submit = SubmitField('Update')
routes.py:
@app.route('/featureEngineering/<id>',methods=['GET','POST'])
@login_required
def featureEngineering(id): # feature engineering ID
featureEngineering = FeatureEngineering.query.filter_by(id=id).first_or_404()
form = {}
for i in featureEngineering.columns:
form[i.id] = FeatureColumnForm()
if form[i.id].validate_on_submit():
if (str(form[i.id].column_id.data) ==str(i.id)):
feature_column = FeatureColumn.query.filter_by(id=i.id).first_or_404()
feature_column.action = form[i.id].action.data
db.session.commit()
flash('updated')
return redirect(url_for('featureEngineering',id=id))
form[i.id].column_id.data = i.id
form[i.id].action.data = FeatureColumn.query.filter_by(id=i.id).first_or_404().action
featureEngineering = FeatureEngineering.query.filter_by(id=id).first_or_404()
return render_template('featureEngineering.html',featureEngineering = featureEngineering,form=form)
相关模板部分:
{% for column in featureEngineering.columns %}
<tr>
<td>{{ column.id }}</td>
<td>{{ column.sample }}</td>
<form action="" method="post">
{{ form[column.id].hidden_tag() }}
<td>{{ form[column.id].action(size=5) }} {{ form[column.id].submit() }}</td>
</form>
<td>{{ column.val_type }}</td>
<td>{{ column.val_count }}</td>
</tr>
{% endfor %}
但是我仍然想知道一次提交是否可以编辑多个。
谢谢 拉尔夫