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
merged 1 commit into from Apr 27, 2021

Conversation

antonstakhouski
Copy link
Contributor

@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

@dataclass
class B(BaseModel):
    bar: str = None

@dataclass
class A(BaseModel):
    foo: str = None
    nested: dict = None
    
    @property
    def nested_(self):
          return B.from_dict(self.nested)
    
    @property
    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()
         form.populate_obj(obj)
         obj.save()

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

@azmeuk
Copy link
Member

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?

@davidism
Copy link
Member

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
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants