def _load_topic_children(topic): from passage_list import PassageList from passage_entry import PassageEntry child_query = "select %s from %s where parent = ? order by order_number" subtopics = [] fields = ", ".join('"%s"' % name for name in ["id",] + PassageList.__fields_to_store__) for row in connection.execute(child_query % (fields, "topic"), (topic.id,)): subtopic = PassageList("") _load_record(subtopic, row) subtopic.parent = topic _load_topic_children(subtopic) subtopics.append(subtopic) topic.subtopics = subtopics fields = ", ".join(["id",] + PassageEntry.__fields_to_store__) passages = [] for row in connection.execute(child_query % (fields, "passage"), (topic.id,)): passage = PassageEntry(None) passage.parent = topic _load_record(passage, row) passage.parent = topic passages.append(passage) topic.passages = passages