Last active
August 27, 2019 09:29
-
-
Save owen800q/72b2890976f10118f702437adbc5b88c to your computer and use it in GitHub Desktop.
SQLAlchemy helper functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Convert nested model object to dict | |
def my_dict(obj): | |
if not hasattr(obj,"__dict__"): | |
return obj | |
result = {} | |
for key, val in obj.__dict__.items(): | |
if key.startswith("_") and key == 'metadata': | |
continue | |
element = [] | |
if isinstance(val, list): | |
for item in val: | |
element.append(my_dict(item)) | |
else: | |
element = my_dict(val) | |
result[key] = element | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment