Querying fields before merge
Sometimes it can be really nice (read: improve the user experience) to be able to query some fields without merging the value from the form back into an object. An example is when a user changes a input select field in a form which has an attached value. In an app we have, a user has a select list of product numbers. We make the app update the product's name on the page when the product changes.
1 class ProductFieldsetForm(FieldsetForm):
2
3 products_menu = product_factory.all_products_menu()
4
5 def __init__(self, product):
6 self.product_id = product.product_id
7 elements = (
8 Fieldset((
9 SelectField('Product code', self.products_menu, 'product_id',
10 html_attrs={'onchange': 'javascript:product_form.submit()'}),
11 StaticField('Product', product.name),
12 )),
13 )
14 FieldsetForm.__init__(self, 'Product selection', elements)
15
16 self.load(product)
17
18 def update(self):
19 product_code_field = self.get_field('Product code')
20 if self.product_id != product_code_field.get_value():
21 product = product_factory.product_with_product_id(product_code_field.get_value())
22 product_field = self.get_field('Product')
23 product_field.set_value(product.name)
The interesting part is where we query the form for a specific field using its get_field method.
In the app itself, we use: