Skip to content

Commit

Permalink
#2: Add docstring to method
Browse files Browse the repository at this point in the history
  • Loading branch information
keiranjprice101 committed Jul 22, 2019
1 parent 3415ce5 commit a9c83d7
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion common/models/db_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ def to_dict(self):
return dictionary

def to_nested_dict(self, included_relations):
"""
Given related models return a nested dictionary with the child or parent rows nested.
:param included_relations: string/list/dict - The related models to include.
:return: A nested dictionary with the included models
"""
dictionary = {}
for column in self.__table__.columns:
dictionary[column.name] = str(getattr(self, column.name))
Expand All @@ -30,7 +37,7 @@ def to_nested_dict(self, included_relations):
if attr in included_relations:
relation = getattr(self, attr)
if isinstance(relation, EntityHelper):
dictionary[attr + "_ID"] = relation.to_dict() # if this was .to_nested_dict() it will make dictionaries all the way down
dictionary[attr + "_ID"] = relation.to_dict()
elif isinstance(relation, InstrumentedList): # Instrumented list is when the inclusion is a child
dictionary[attr + "_ID"] = []
for entity in getattr(self, attr):
Expand Down

0 comments on commit a9c83d7

Please sign in to comment.