我正在尝试渲染一个包含3个文本字段和一个选择字段的表单。我可以使用数据库中的数据渲染文本字段,但不能使用选择字段。 选择字段必须将来自数据库的数据和其他选项作为选定选项提供。 我正在使用: 烧瓶的SQLAlchemy == 2.1 烧瓶-WTF == 0.12
这是代码:
@app.route('/gateway/edit/<int:gwt_id>', methods=['GET','POST'])
def gateway_edit(gwt_id):
q = Device.query.filter_by(gtw_id=gwt_id).first()
form = Gateway(obj=q)
form.populate_obj(q)
return render_template('gateway.html', form=form)
class Gateway(db.Model):
__tablename__ = "gateway"
gateway_id = db.Column(db.Integer, primary_key=True)
gateway_name = db.Column(db.String(64))
gateway_desc = db.Column(db.String(64))
devices = db.relationship('Device', backref='gateway', lazy='dynamic')
class Device(db.Model):
__tablename__ = "device"
device_id = db.Column(db.Integer, primary_key=True)
device_name = db.Column(db.String(64))
device_desc = db.Column(db.String(64))
gateway_id = db.Column(db.Integer,db.ForeignKey('gateway.gateway_id'))
device_ip = db.Column(db.String(64))
class Gateway(Form):
gateway_name = StringField('Gateway Name')
gateway_desc = TextAreaField('Gateway Description')
def gtws():
return Gateway.query
class Device(Form):
device_name = StringField('Device Name')
device_desc = TextAreaField('Device Description')
device_ip = StringField('Device IP')
gtw_group = QuerySelectField('Gateway',query_factory=gtws)
{{ render_field(form.device_name, class="form-control")}}
{{ render_field(form.device_desc, class="form-control")}}
{{ render_field(form.device_ip, class="form-control")}}
{{ render_field(form.gtw_group, class="form-control")}}
提前致谢,
答案 0 :(得分:0)
我发现了问题。我误解了模型中的反射。它在我的设备模型中创建一个虚拟列。然后,我可以使用此虚拟列名称关联表单字段(在我的示例中为QuerySelectField)。
我在代码中做了一些简单的更改: 的模型强>
var imaVariable = "I'll get added into the string.";
var newString = "This is a string" + imaVariable + "This is also a string";
<强>表格强>
devices = db.relationship('Device', backref='gtw', lazy='dynamic')
<强>模板强>
gtw = QuerySelectField('Gateway',query_factory=gtws)