I'm trying to convert my SQL Alchemy to a basic dict.
For this case, I'm trying to use the __dict__
builtin function.
But the behavior is quite strange.
Here is my function:
def trial_to_dict(sa_trial_instance):
""""Convert a SQL Alchemy instance to a JSON dict
params:
sa_trial_instance (dict): SQL Alchemy instance
returns:
dict: JSON
"""
temp_dict = sa_trial_instance.__dict__
return {
"ct_id": temp_dict["ct_id"],
"study_type": temp_dict["study_type"],
"phase": temp_dict["phase"]
}
sa_trial_instance
is defined, and it's a Sql Alchemy instance
But temp_dict
isn't the result I'm expecting :
Obvioulsy, I'm getting this error:
Exception has occurred: KeyError
'ct_id'
Is anyone has an explanation about this? Thank you for your help!