1

I would like to add a row to an existing table but the column name is unknown. Earlier on in my script I have been able to use setattr() to update an existing entry and to query using getattr() - but I can't figure out how to add an new entry.

As an example: the ean and retailer_sku are read from a csv file, but, depending on the scripts inputs, the retailer column can have different values. I would like to avoid using several if-else statements.

ean='7622210496034'
retailer = 'gh'
retailer_sku = '8222868'
retailer_add = SkuRetailer(mainean=ean, retailer=retailer_sku)
db.session.add(retailer_add)

How can I use 'gh' instead of retailer in the constructor?

1 Answer 1

1

To use variable keyword arguments (or kwargs), a common workaround is the following:

retailer = 'gh'
sku = '8222868'
kwargs = { 'mainean': ean, retailer: sku }
retailer_add = SkuRetailer(**kwargs)
db.session.add(retailer_add)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.