views:

104

answers:

1

I am trying to set up form widgets for adding some objects to the database but I'm getting stuck because it seems impossible to pass any arguments to Widgets contained within a WidgetList. To clarify that, here is my WidgetList:

class ClientFields(forms.WidgetsList):
    """Form to create a client"""

    name = forms.TextField(validator=validators.NotEmpty())
    abbreviated = forms.TextField(validator=validators.NotEmpty(), attrs={'size':2})
    address = forms.TextArea(validator=validators.NotEmpty())
    country = forms.TextField(validator=validators.NotEmpty())
    vat_number = forms.TextField(validator=validators.NotEmpty())
    email_address = forms.TextField(validator=validators.Email(not_empty=True))
    client_group = forms.SingleSelectField(validator=validators.NotEmpty(),
                                       options=[(g.id, g.name) for g in ClientGroup.all_client_groups().all()])

You see I have had to resort to grabbing objects from the database from within the WidgetList, which means that it's rather tightly coupled with the database code (even though it's using a classmethod in the model).

The problem is that once the WidgetList instance is created, you can't access those fields (otherwise I could just call client_fields.client_group.options=[(key,value)] from the controller) - the fields are removed from the class and added to a list, so to find them again, I'd have to iterate through that list to find the Field class I want to alter - not clean. Here's the output from ipython as I check out the WidgetsList:

In [8]: mad.declared_widgets
Out[8]: 
[TextField(name='name', attrs={}, field_class='textfield', css_classes=[], convert=True),
 TextField(name='abbreveated', attrs={'size': 2}, field_class='textfield', css_classes=[], convert=True),
 TextArea(name='address', rows=7, cols=50, attrs={}, field_class='textarea', css_classes=[], convert=True),
 TextField(name='country', attrs={}, field_class='textfield', css_classes=[], convert=True),
 TextField(name='vat_number', attrs={}, field_class='textfield', css_classes=[], convert=True),
 TextField(name='email_address', attrs={}, field_class='textfield', css_classes=[], convert=True),
 SingleSelectField(name='client_group', attrs={}, options=[(1, u"Proporta's Clients")], field_class='singleselectfield', css_classes=[], convert=False)]

So...what would be the right way to set these Widgets and WidgetLists up without coupling them too tightly to the database and so on?

A: 

A page in the TurboGears documentation may help.

kylebrooks
This code is based on all that code in that page, yes. But it doesn't have any DB-code at all - i.e. the fields are all static. There are no SingleSelectFields in there...
William Roe