如何最好地将数据库对象传递到涡轮齿轮 WidgetList?
我正在尝试设置表单小部件以将一些对象添加到数据库中,但我陷入困境,因为似乎不可能将任何参数传递给 WidgetList 中包含的小部件。 为了澄清这一点,这是我的 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()])
您会看到,我不得不从 WidgetList 中的数据库中获取对象,这意味着它与数据库代码紧密耦合(即使它在模型中使用类方法)。
问题是,一旦创建了 WidgetList 实例,您就无法访问这些字段(否则我可以从控制器调用 client_fields.client_group.options=[(key,value)] ) - 这些字段将从类中删除,并且添加到列表中,因此要再次找到它们,我必须遍历该列表才能找到我想要更改的 Field 类 - 不干净。 这是我检查 WidgetsList 时 ipython 的输出:
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)]
那么...设置这些 Widgets 和 WidgetLists 而不将它们与数据库等耦合得太紧密的正确方法是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TurboGears 文档中的页面可能会有所帮助。
A page in the TurboGears documentation may help.