class BaseGroup(Base): # ... parent_id = Column(Integer, ForeignKey('base_groups.id')) parent = relationship('BaseGroup', foreign_keys='BaseGroup.id', primaryjoin='BaseGroup.parent_id == BaseGroup.id', uselist=False) child_groups = relationship('BaseGroup', foreign_keys='BaseGroup.parent_id', primaryjoin='BaseGroup.id == BaseGroup.parent_id', uselist=True) roster = relationship("BaseGroupRoster", order_by="BaseGroupRoster.position", collection_class=ordering_list('position', count_from=1), lazy='joined') @hybrid_property def sub_roster(self): roster = self.roster for each in self.child_groups: print(each.name) roster += each.sub_roster return roster