wtforms CRUD - set default in SelectField

[亡魂溺海] 提交于 2021-01-29 15:07:00

问题


I have a CRUD application where wtforms is being used to populate a series of forms with a 'for' loop to allow users to edit existing data in the DB. I'm using "value=row.XXX" to pre-populate the form with existing data from the DB as a default. This works well for normal StringFields, but doesn't work for SelectField. Can anyone help!?

Example html below. The form_edit.group is a SelectField. Annoyingly, when displayed in the form, it defaults to the first item in the 'choices' list rather than the previously chosen data (value=row.group doesn't work as it does for StringFields). This means that when the user comes to resubmit the form it defaults to this first item.

                    <div class="form-group col-sm-6">
                        {{ form_edit.description.label }}
                        {{ form_edit.description(class="form-control", value=row.description) }}
                        {% for error in form_edit.description.errors %}
                        <span style="color: red;">[{{ error }}]</span>
                        {% endfor %}
                    </div>
                    <div class="form-group col-sm-6">
                        {{ form_edit.group.label }}
                        {{ form_edit.group(class="form-control", value=row.group) }}
                        {% for error in form_edit.group.errors %}
                        <span style="color: red;">[{{ error }}]</span>
                        {% endfor %}
                    </div>
                </div>
                <div class="row">
                    <div class="form-group col-sm-6">
                        {{ form_edit.qty.label }}
                        {{ form_edit.qty(class="form-control", value=row.qty) }}
                        {% for error in form_edit.qty.errors %}
                        <span style="color: red;">[{{ error }}]</span>

Forms:

class Areas_form(FlaskForm):

    hidden_id = HiddenField('Area id')
    description = StringField('Description',validators=[DataRequired()])
    group = SelectField('Group',validators=[DataRequired()],choices=['Ext(Am) Wall','Ext(Gnd) Wall','Roof(Am)','Flr Slab','Flr(Am)'])
    qty = FloatField('Quantity', validators=[DataRequired(message='must be a number')], default=float(1))
    lth_a = FloatField('Length a', validators=[DataRequired(message='must be a number')])
    lth_b = FloatField('Length b', validators=[DataRequired(message='must be a number')])                                 
    assembly = SelectField('Assembly',validators=[DataRequired()], choices=[])
    dev_nth = FloatField('Deviation from North', validators=[InputRequired(message='must be a number')]) 
    ang_hor = FloatField('Angle from horizontal', validators=[InputRequired(message='must be a number')])
    submit = SubmitField('Submit data')

Example row:

When I press "edit" the following form comes up. The "group" "Flr Slab", has defaulted back to the first item in the choices list - "Ext(Am) Wall". By contrast the "description" field has pulled "Floor 1" from the database.


回答1:


Update your SelectField difinition in class Areas_form(FlaskForm): as follows:

group = SelectField('Group',validators=[DataRequired()],choices=[(1,'Ext(Am) Wall'),(2,'Ext(Gnd) Wall'),(3,'Roof(Am)'),(4,'Flr Slab'),(5,'Flr(Am)')])

where we assign the value option as well as the appeared text, and finally, then in your code after form submission, you get the value as regular column fields

Notice, you will use numbers 1,2,3,4, or 5 as a mapping for your choices in select, which would be better.

Adding upon commit

In order to set the default value update

{{ form_edit.group(class="form-control", value=row.group) }}

to

{{ form_edit.group(class="form-control", value=row.group.data) }}

if it does not work, check that data type of type(row.group.data) in python, if it is string type, update the group column difiniation to:

group = SelectField('Group',validators=[DataRequired()],choices=[('1','Ext(Am) Wall'),('2','Ext(Gnd) Wall'),('3','Roof(Am)'),('4','Flr Slab'),('5','Flr(Am)')])

In case, we force to use integer value, add coerce=int to the end of my first suggestion before additions as follows:

group = SelectField('Group',validators=[DataRequired()],choices=[(1,'Ext(Am) Wall'),(2,'Ext(Gnd) Wall'),(3,'Roof(Am)'),(4,'Flr Slab'),(5,'Flr(Am)'),coerce=int])

looking for to hearing from you :)

Good Luck



来源:https://stackoverflow.com/questions/65516397/wtforms-crud-set-default-in-selectfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!