Python: SelectField "Invalid selection"

form.py

class Confirm(CSRFForm):
    monitor_updates = SelectField(
        lazy_gettext("Monitor updates of an app"),
        validators=[validators.Optional()], choices=[], coerce=int
    )

      

view.py

def upload_confirm():
    form = Confirm()

    if form.validate_on_submit:
        if form.monitor_updates.data == 0:
            current_workflow.installation_source.monitor_updates_id = None
            db.session.commit()
        else:
            current_workflow.installation_source.monitor_updates_id = form.monitor_updates.data
            db.session.commit()

    choices = [(app[1][0], app[1][1]) for app in list_choices]
    form.monitor_updates.choices =[(0, "Nothing matches")]
    form.monitor_updates.choices += choices

      

models.py

class InstallationSource(db.Model):

    monitor_updates_id = db.Column(db.Integer, db.ForeignKey('updates_software.id'))

      

When I fill out the form and select "Nothing matches" in the dropdown menu with a value of 0, I get an error.

screenshot

If you choose a different value, the save is correct. I can only use the number 0

, because all other numbers may be busy

+3


source to share


1 answer


Please move

    choices = [(app[1][0], app[1][1]) for app in list_choices]
    form.monitor_updates.choices =[(0, "Nothing matches")]
    form.monitor_updates.choices += choices

      



what he was before form.validate_on_submit()

def upload_confirm():
    form = Confirm()

    choices = [(app[1][0], app[1][1]) for app in list_choices]
    form.monitor_updates.choices =[(0, "Nothing matches")]
    form.monitor_updates.choices += choices

    if form.validate_on_submit():
        if form.monitor_updates.data == 0:
            current_workflow.installation_source.monitor_updates_id = None
            db.session.commit()
        else:
            current_workflow.installation_source.monitor_updates_id = form.monitor_updates.data
            db.session.commit()

      

+1


source







All Articles