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

Does not work with flask-admin due to override of formdata #24

Open
cybertoast opened this issue May 30, 2014 · 1 comment
Open

Does not work with flask-admin due to override of formdata #24

cybertoast opened this issue May 30, 2014 · 1 comment

Comments

@cybertoast
Copy link

The specific issue is that if wtforms_json is used, the monkey patch does not allow fallthrough/bubble-up of different datatypes.

For example, if using flask-admin and a StringField is overridden on the form as a FileUploadField, the formdata processor will normally parse the provided data, save the file, and store the filename to the StringField. The monkey patch breaks this process since it transforms all input types into their text representation via six:

    if isinstance(self, TextField):
        if self.data is None:
            self.data = u''
        else:
            self.data = six.text_type(self.data)

This is obviously incorrect. I'm not sure about the motivations for this code, so can't say what the correct solution would be. I actually suspect that this block of code should be removed altogether, since it's certainly possible that the back-end should store no value if that's what's received - rather than a blank string. Or it should be handled as the model expects, rather than as overridden text.

I'm happy to submit a pull request, but wanted to first understand what the intentions were.

@alanhamlett
Copy link
Contributor

WTForms 2 no longer has the TextField alias.

StringField from WTForms on this line mangles None to a string not WTForms-JSON. For the same reason that you say, I use a custom StringField class that accepts None:

class StringField(Field):
    widget = widgets.TextInput()

    def process_formdata(self, valuelist):
        if valuelist:
            self.data = valuelist[0]
        else:
            self.data = None

    def _value(self):
        """Provides value displayed in HTML form.
        """
        return text_type(self.data) if self.data is not None else ''

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

No branches or pull requests

2 participants