Skip to content

Instantly share code, notes, and snippets.

@owen800q
Last active August 27, 2019 09:29
Show Gist options
  • Save owen800q/72b2890976f10118f702437adbc5b88c to your computer and use it in GitHub Desktop.
Save owen800q/72b2890976f10118f702437adbc5b88c to your computer and use it in GitHub Desktop.
SQLAlchemy helper functions
# 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