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

Always call setattr in FormField.populate #675

merged 1 commit into from Apr 27, 2021


Copy link

@antonstakhouski antonstakhouski commented Dec 28, 2020

I'm writing a flask website that communicates with an API gateway using json. I wrote a some ORM models using python dataclasses that convert json to objects and vice versa and hide all data manipulation stuff.

The problem is that i could receive a nested json: {"foo": 42, "nested": {"bar": "baz"}}. And i map it to something like

class B(BaseModel):
    bar: str = None

class A(BaseModel):
    foo: str = None
    nested: dict = None
    def nested_(self):
          return B.from_dict(self.nested)
    def nested_(self, value):
        self.nested = value.to_dict

And form is:

class NestedForm(FlaskForm):
   bar = StringField('Bar')

class MyForm(FlaskForm):
   foo = StringField('Foo')
   nested_ = FormField(NestedForm, 'Nested')

In routes I have code like this:

def update_a():
    form = MyForm()
    if form.validate_on_submit:
         obj = A()

FormField will not modify obj.nested (it won't write temporary candidate object to obj attribute).

Copy link

azmeuk commented Apr 27, 2021

I played a bit with your patch and I could not find anything wrong with this.
@davidism any thougths on this?

Copy link

Seems fine, go ahead and merge it when ready.

@azmeuk azmeuk merged commit 4d9e6f8 into wtforms:master Apr 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

Successfully merging this pull request may close these issues.

None yet

3 participants