{"id":650,"date":"2009-06-13T12:16:59","date_gmt":"2009-06-13T16:16:59","guid":{"rendered":"http:\/\/cd34.com\/blog\/?p=650"},"modified":"2009-06-13T12:16:59","modified_gmt":"2009-06-13T16:16:59","slug":"recursive-category-table-in-python","status":"publish","type":"post","link":"https:\/\/cd34.com\/blog\/programming\/python\/recursive-category-table-in-python\/","title":{"rendered":"Recursive Category Table in Python"},"content":{"rendered":"<p>While working with a project, the problem with the recursive category table being built came up.  The table holds the parent_id of the category name and the result is a list with the id and the name of the category.  The category name is prepended with spaces equivalent to the level of indentation.<\/p>\n<p>model:<\/p>\n<pre>\r\nclass AchievementCategory(DeclarativeBase):\r\n\t__tablename__ = 'achievement_categories'\r\n\r\n\tid = Column(mysql.MSBigInteger(20, unsigned = True), primary_key = True)\r\n\tparent_id = Column(mysql.MSBigInteger(20, unsigned = True), default = 0)\r\n\tname = Column(Unicode(80))\r\n<\/pre>\n<p>code:<\/p>\n<pre>\r\ndef get_cats(n = 0, c_list = [], level = 0):\r\n\tsql = DBSession.query(AchievementCategory).filter_by(parent_id = n).order_by(AchievementCategory.name).all()\r\n\tfor e in sql:\r\n\t\tc_list.append([e.id, level * \" \" + e.name, level])\r\n\t\tget_cats(e.id, c_list, level+1)\r\n\treturn c_list\r\n\r\nprint get_cats()\r\n<\/pre>\n<div style=\"float:left;\">\n<div id=\"fb-root\"><\/div>\n<fb:like href=\"https:\/\/cd34.com\/blog\/programming\/python\/recursive-category-table-in-python\/\" width=\"250\" send=\"false\" show_faces=\"false\" layout=\"button_count\" action=\"recommend\"><\/fb:like>\n<\/div><div style=\"clear:both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>While working with a project, the problem with the recursive category table being built came up. The table holds the parent_id of the category name and the result is a list with the id and the name of the category. The category name is prepended with spaces equivalent to the level of indentation. model: class [&hellip;]<\/p>\n<div style=\"float:left;\">\n<div id=\"fb-root\"><\/div>\n<fb:like href=\"https:\/\/cd34.com\/blog\/programming\/python\/recursive-category-table-in-python\/\" width=\"250\" send=\"false\" show_faces=\"false\" layout=\"button_count\" action=\"recommend\"><\/fb:like>\n<\/div><div style=\"clear:both;\"><\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[41,40],"class_list":["post-650","post","type-post","status-publish","format-standard","hentry","category-python","tag-category-table","tag-recursive"],"_links":{"self":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts\/650","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/comments?post=650"}],"version-history":[{"count":1,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts\/650\/revisions"}],"predecessor-version":[{"id":651,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts\/650\/revisions\/651"}],"wp:attachment":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/media?parent=650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/categories?post=650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/tags?post=650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}