使用以下模型:
class Recipe(db.Model):
__tablename__ = 'recipe'
__searchable__ = ['description']
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(128), index=True, unique=True, nullable=False)
description = db.Column(db.String(128))
style = db.Column(db.Enum('fried', 'baked', 'roasted', 'mixed', name='cooking_style'))
type = db.Column(db.Enum('breakfast', 'lunch', 'dinner', 'snack', 'sauce', 'bread', 'dessert', name='recipe_type'))
以下内容:
form = CreateRecipeForm()
return render_template('create_client_recipe.html', form=form, client=c, recipe=r)
我如何代表style
& type
(两个db.Enum
字段)作为WTForm中的选择字段?
答案 0 :(得分:1)
不知道你是否需要但是我正在使用Flask-SQLAlchemy,我仍然是初学者,但我希望这可以帮助你基本上我创建了一个名为状态的Enum,其值为'Active'且“非活动”,我想将这些值放在一个表单中,但我想从数据库中获取值。
我的模特这个:
class StationhasBots(db.Model):
"Many to Many table one raio station will have many functions"
__tablename__ = 'station_has_bots'
fk_radio_station_id = db.Column(db.ForeignKey('radio_station.id'), primary_key=True)
fk_bot_functions_id = db.Column(db.ForeignKey('bot_functions.id'), primary_key=True)
#Function is active or not
state = db.Column(db.Enum('Active','Inactive',name='estado'),nullable=False)
#In which time it will run
run_frequency = db.Column(db.String(STRING_LEN),nullable=False)
next_run = db.Column(db.DateTime(timezone=True),nullable=False)
#Source to fetch information
source = db.Column(db.String,nullable=False)
#path to the file that will be executed to pull info.
path = db.Column(db.String,nullable=True)
function_of_bots = db.relationship("BotsFunctions", backref=db.backref('function_from_bots'))
这是我的表格:
class AddBotForm(Form):
station = QuerySelectField(query_factory=all_stations, allow_blank=False, blank_text='- select station-')
function = QuerySelectField(query_factory=all_bot_functions, allow_blank=False, blank_text='- select function-')
#state = SelectField(choices=[('active', 'Active'), ('inactive', 'Inactive')])
state = SelectField(choices=[(g, g)for g in StationhasBots.state.property.columns[0].type.enums]) #Get the state from Station_has_Bots Table.
next_run = DurationField(description=_("Duration , in HH:MM(:SS)"))
run_frequency = HiddenField()
source = StringField()
path = StringField()
submit = SubmitField(_('Save'))
在这种形式中,您可以看到在状态字段中我运行查询,此查询将获得在创建数据库时创建的Enum调用状态。
要渲染表单,我只是在我的视图中执行此操作
@radio.route('/bots/add/', methods=['GET', 'POST'])
@login_required
def bot_function_add():
"""Renders the form"""
form = AddBotForm(request.form)
program = None
return render_template('radio/bot.html', program=program, form=form)
然后在模板中做了这个
<h2>{{ _('Add') }} {{ _('Bot') }}</h2>
<form method="POST" action=""/>
{{ form.hidden_tag() }}
{{ render_field(form, form.station) }}
{{ render_field(form, form.function) }}
{{ render_field(form, form.next_run) }}
{{ render_field(form, form.state) }}
{{ render_field(form, form.source) }}
{{ render_field(form, form.path) }}
{{ render_field(form, form.submit) }}
</form>
Type = SelectField(choices=[(g, g)for g in Recipe.type.property.columns[0].type.enums])
优先g - &gt;保留价值(在“HTML代码”中)
秒g - &gt;是什么将呈现给用户。
然后在create_client_recipe.html文件中呈现表单 你只需要做一些像
这样的事情{{ render_field(form, form.Type) }}
我知道自从你的帖子希望这可以帮助其他人后已经过去了六个月。