Jan
Jan

Reputation: 43169

sqlalchemy - save multiple values at once

In sqlalchemy I have a base class Stock.
Now I'd like to bind some previously scraped items and want to bind them like so:

stock = Stock(isin = item['isin'], name = item['name'], points = item['points'], ...)

This feels very unpythonic and boring.
The keys from item are named exactly like the internal representation of Stock, however some items need to be ignored (these start with an underscore, e.g. item['_leave_me_alone']).
Is there a way to write the above in a better way?

Upvotes: 0

Views: 66

Answers (1)

Ilja Everilä
Ilja Everilä

Reputation: 52929

You could adapt the method used here, or in other words unpack a filtered dict as arguments:

stock = Stock(**{k: v for k, v in item.items() if not k.startswith('_')})

Upvotes: 2

Related Questions