Skip to content

Selected value issue with QuerySelectMultipleField in edit form #36

Open
@aminalaee

Description

@aminalaee

When working with QuerySelectMultipleField , the empty form works ok,
but when the edit form is built from an object like form = MyForm(obj=obj) then the multiple select field does not show the selected field.

In other words it always shows none of the options to be selected but in reality there could be some of them selected.

I think the fix is in the data property here:

https://github.com/wtforms/wtforms-sqlalchemy/blob/c0d741429407f0bbd2e66e28528e0bada4769ac4/wtforms_sqlalchemy/fields.py#L166-L179

Which instead should be like:

    def _get_data(...):
        formdata = self._formdata
        if formdata is not None:
            data = []
            for pk, obj in self._object_list:
                if not formdata:
                    break
                elif pk in formdata:
                    formdata.remove(pk)
                    data.append(obj)
            if formdata:
                self._invalid_formdata = True
            self.data = data or self._data  # This change here fixed it
        return self._data

I don't know if a PR for this would be accepted or not, I could do that anyway.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions