{"id":641,"date":"2009-06-10T12:31:20","date_gmt":"2009-06-10T16:31:20","guid":{"rendered":"http:\/\/cd34.com\/blog\/?p=641"},"modified":"2009-06-15T14:02:31","modified_gmt":"2009-06-15T18:02:31","slug":"turbogears-tableform-and-a-callable-option-to-a-widget","status":"publish","type":"post","link":"https:\/\/cd34.com\/blog\/programming\/python\/turbogears-tableform-and-a-callable-option-to-a-widget\/","title":{"rendered":"TurboGears, Tableform and a callable option to a Widget"},"content":{"rendered":"<p>While doing some TurboGears development I ran into an issue where I needed to generate a select field&#8217;s options from the database that was dependent on authentication.  Since defining the query in the model results in a cached result when the class is instantiated, the query couldn&#8217;t be defined there.  There are multiple mentions of using a callable to deal with this situation, but, no code example.<\/p>\n<p>From this posting in <a href=\"http:\/\/groups.google.com\/group\/turbogears\/browse_thread\/thread\/b390280051e0f7d8\/\">Google Groups for TurboGears<\/a>, we were able to figure out the code that made this work.<\/p>\n<p>template:<\/p>\n<pre>&lt;div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"\r\n      xmlns:py=\"http:\/\/genshi.edgewall.org\/\"\r\n      xmlns:xi=\"http:\/\/www.w3.org\/2001\/XInclude\" \r\n      py:strip=\"\">\r\n\r\n${tmpl_context.form(value=value)}\r\n\r\n&lt;\/div><\/pre>\n<p>controller:<\/p>\n<pre>    @expose('cp.templates.template')\r\n    def form(self):\r\n        c.form = TestForm()\r\n        c.availips = [[3,3],[2,2]]\r\n        return dict(template='form',title='Test Form',value=None)<\/pre>\n<p>model:<\/p>\n<pre>from pylons import c\r\n\r\ndef get_ips():\r\n    return c.availips\r\n\r\nclass TestForm(TableForm):\r\n    action = '\/test\/testadd'\r\n    submit_text = 'Add test'\r\n\r\n    class fields(WidgetsList):\r\n        User = TextField(label_text='FTP Username', size=40, validator=NotEmpty())\r\n        Password = PasswordField(label_text='FTP Password', size=40, validator=NotEmpty())\r\n        ip = SingleSelectField(label_text=\"Assign to IP\",options=get_ips)\r\n<\/pre>\n<div style=\"float:left;\">\n<div id=\"fb-root\"><\/div>\n<fb:like href=\"https:\/\/cd34.com\/blog\/programming\/python\/turbogears-tableform-and-a-callable-option-to-a-widget\/\" 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 doing some TurboGears development I ran into an issue where I needed to generate a select field&#8217;s options from the database that was dependent on authentication. Since defining the query in the model results in a cached result when the class is instantiated, the query couldn&#8217;t be defined there. There are multiple mentions of [&hellip;]<\/p>\n<div style=\"float:left;\">\n<div id=\"fb-root\"><\/div>\n<fb:like href=\"https:\/\/cd34.com\/blog\/programming\/python\/turbogears-tableform-and-a-callable-option-to-a-widget\/\" width=\"250\" send=\"false\" show_faces=\"false\" layout=\"button_count\" action=\"recommend\"><\/fb:like>\n<\/div><div style=\"clear:both;\"><\/div>","protected":false},"author":15,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[37,38,36,39],"class_list":["post-641","post","type-post","status-publish","format-standard","hentry","category-python","tag-tableform","tag-toscawidgets","tag-turbogears","tag-widget"],"_links":{"self":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts\/641","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\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/comments?post=641"}],"version-history":[{"count":9,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts\/641\/revisions"}],"predecessor-version":[{"id":652,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts\/641\/revisions\/652"}],"wp:attachment":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/media?parent=641"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/categories?post=641"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/tags?post=641"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}