title: dynamically insert rows to table flask sqlalchemy
body: <p>I want to insert new rows to an sql table dynamically in SQLAlchemy.</p>
  
  <p>I basically want to do <a href="https://stackoverflow.com/questions/37369686/how-can-i-use-a-string-to-to-represent-an-sqlalchemy-object-attribute">this</a>, but instead of doing it with an UPDATE, I want to do it with an INSERT.</p>
  
  <p>I am using Flask and WTForms.</p>
  
  <p>Right now, I have to add a new user doing this:</p>
  
  <pre><code>if form.validate_on_submit():
      contact_info = UserContactInfo(street_number=form.street_number.data,
                                     street_address = form.street_address.data,
                                     unit_number = form.unit_number.data
                                    )
      db.session.add(contact_info)
      db.session.commit()
  </code></pre>
  
  <p>I would be able to add new attributes to UserContactInfo alot easier if I could iterate through the Inserts.</p>
  
  <p>I have tried to do it this way, but no change is made to the database.</p>
  
  <pre><code>form = ContactInfoForm()
  contact_info = UserContactInfo()
  
  if form.validate_on_submit():
      table_obj_members = get_attributes(UserContactInfo())
          for aa in table_obj_members:
               form_data = getattr(form, aa).data
               setattr(contact_info, aa, form_data)
  
   db.session.commit()
  </code></pre>
  
  <p>I have seen a couple discussions around this and instantiating the whole Class and not the object attribute, but I can't seem to make this apply to this problem.</p>
  
  <p>Thanks for any help.</p>