Build a dictionary from an object's explicitly declared fields
My table definition:
class AppProperties(base):
__tablename__ = "app_properties"
key = Column(String, primary_key=True, unique=True)
value = Column(String)
Code for updating an app property:
session = Session()
query = session.query(AppProperties).filter(AppProperties.key == "memory")
new_setting = AppProperties(key="memory", value="1 GB")
query.update(new_setting)
The line query.update(new_setting)
fails because the update method requires an iterable. If I use query.update(vars(new_setting))
, it includes some extra values, which are not there in the underlying table and hence fails.
How can I build this dictionary: {"key": "memory", "value": "1 GB"}
using the object new_setting
, so that I can call the update
method using this dictionary?
For example:
query.update({"key": "memory", "value": "1 GB"})
Because I already have everything in new_setting
, I just need to convert it into a dictionary with only those keys which are explicitly declared in the class definition of AppProperties
, without inheriting the fields from the base class or any other scope.
Comments
Post a Comment