I am trying to loop through a dict that I received via json and save the results to a database via sqlalchemy. I've kept the field names the same as the key names from the dict. It seems repetitive to list each field and dict over and over. But when I try to use something like c.keys() it does not work. If that were the case I could do: for key in c.keys(): customer.key = c[key]
But that does not work. My current code is:
for c in response['Customers']:
customer = Customer()
customer.ID = c['ID']
customer.Name = c['Name']
customer.Currency = c['Currency']
customer.PaymentTerm = c['PaymentTerm']
customer.Discount = c['Discount']
customer.TaxRule = c['TaxRule']
customer.Carrier = c['Carrier']
session.add(customer)
session.commit()
c.keys()?for key in c.keys(): setattr(customer, key, c[key]), something like that... or trydir(customer), that may give you more than you wanted though...customer._sa_instance_state.attrs.keys()looks like it will give them to you as well....for key in c.keys(): setattr(customer, key, c[key])worked perfectly thanks!