code: def expand(obj, relation, seen):
      
  
      if hasattr(relation, 'all'):
          relation = relation.all()
  
      if hasattr(relation, '__iter__'):
          return [expand(obj, item, seen) for item in relation]
  
      if type(relation) not in seen:
          return to_json(relation, seen + [type(obj)])
      else:
          return relation.id
docs: Return the to_json or id of a sqlalchemy relationship.