Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selected value issue with QuerySelectMultipleField in edit form #36

Open
aminalaee opened this issue Feb 18, 2022 · 1 comment
Open

Comments

@aminalaee
Copy link

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:

def _get_data(self):
formdata = self._formdata
if formdata is not None:
data = []
for pk, obj in self._get_object_list():
if not formdata:
break
elif pk in formdata:
formdata.remove(pk)
data.append(obj)
if formdata:
self._invalid_formdata = True
self._set_data(data)
return self._data

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.

@aminalaee
Copy link
Author

BTW, thanks a lot for the work here, I really appreciate that.
I've used most of this code in my project (which I need to put a reference it was used from this project), the reason I didn't directly use wtforms-sqlalchemy was that I wanted to support SQLAlchemy > 1.4 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant