Events

On Change

当一个字段的内容发生改变时,on_change属性定义个方法就会被调用。

这个方法至少有几个参数:cr,uid,ids,这是三个传统的参数并且也在上下文字典中。你需要给方法添加参数。他们必须符合在视图中定义的其他字段,并且必须在XML中以以下的方式定义::

  1. <field name="name_of_field" on_change="name_of_method(other_field'_1_', ..., other_field'_n_')"/>

下面的是来自销售订单视图中的例子。

你可以使用‘context’关键字来访问上下文中的数据,这个上下文可以被用作函数中的参数::

  1. <field name="shop_id" select="1" on_change="onchange_shop_id(shop_id)"/>
  1. def onchange_shop_id(self, cr, uid, ids, shop_id):
  2. v={}
  3. if shop_id:
  4. shop=self.pool.get('sale.shop').browse(cr,uid,shop_id)
  5. v['project_id']=shop.project_id.id
  6. if shop.pricelist_id.id:
  7. v['pricelist_id']=shop.pricelist_id.id
  8. v['payment_default_id']=shop.payment_default_id.id
  9. return {'value':v}

当编辑shop_id表单字段时,sale_order对象的onchange_shop_id方法会被调用,并且返回一个字典,其中‘value’关键字包含在’project_id’, ‘pricelist_id’, ‘payment_default_id’字段中新值来使用的字典。

我们注意到除了字段值,更改更多的东西是可能的。例如,可以更改字段值和其他字段的domain,这个更改通过表单的返回值:return {‘domain’: d, ‘value’: value}

context:

in <record model=”ir.actions.act_window” id=”a”> you can add a context field, which will be pass to the action.

See the example below:

  1. <record model="ir.actions.act_window" id="a">
  2. <field name="name">account.account.tree1</field>
  3. <field name="res_model">account.account</field>
  4. <field name="view_type">tree</field>
  5. <field name="view_mode">form,tree</field>
  6. <field name="view_id" ref="v"/>
  7. <field name="domain">[('code','=','0')]</field>
  8. <field name="context">{'project_id': active_id}</field>
  9. </record>

view_type:

  1. tree = (tree with shortcuts at the left), form = (switchable view form/list)

view_mode:

  1. tree,form : sequences of the views when switching

Getting Defaults

Description:

Get back the value by default for one or several fields.

Signature: def default_get(self, cr, uid, fields, form=None, reference=None)

Parameters:

  • fields: the fields list which we want to recover the value by default.

  • form (optional): TODO

  • reference (optional): TODO

Returns: dictionary of the default values of the form {‘field_name’: value, … }

Example:

  1. self.pool.get('hr.analytic.timesheet').default_get(cr, uid, ['product_id','product_uom_id'])

default_set

Description:

Change the default value for one or several fields.

Signature: def default_set(self, cr, uid, field, value, for_user=False)

Parameters:

  • field: the name of the field that we want to change the value by default.

  • value: the value by default.

  • for_user (optional): boolean that determines if the new default value must be available only for the current user or for all users.

Returns: True

Example:

  1. TODO