表单可包含按钮,用于执行动作
一个按钮的基本属性有:
string:定义按钮显示文本
type:执行的动作类型,object,action,workflow,默认是 workflow
workflow 用于触发工作流引擎信号;object 用于调用Python方法;action 用于运行窗口动作。
name:所运行动作的ID,触发的方法标识。工作流信号名、模型方法名称或运行窗口动作act_window的数据库ID。
class:应用 CSS 样式的可选属性,与HTML中用法相同
icon:可用的icon在 addons/web/static/src/img/icons
args:传递方法的参数
content :将值添加到上下文,在调用的方法、跳转的视图中可以直接取用。
confirm :弹出对话框,进行点击确认。
special=“cancel” :用于向导视图的按钮,用来取消和关闭向导表单。
states: 可见的状态
classname: 样式类名(常用 oe_highlight高亮显示)
type=“object”
需要在model.py文件中定义一个方法,并且在button的name属性值与方法名相同
添加动作按钮
<header>
<button name="button_check_isbn" type="object"
string="Check ISBN" />
</header>
model添加button_check_isbn方法函数
def button_check_isbn(self):
for book in self:
if not book.isbn:
raise ValidationError('Please provide an ISBN for %s' % book.name)
if book.isbn and not book._check_isbn():
raise ValidationError('%s is an invalid ISBN' % book.isbn)
return True
其中_check_isbn()也是model中的方法,这里就不写了,只是起到辅助判断的作用
type=“action”
用于页面中视图的跳转,name属性为”%(moduleName.id)d”
- moduleName:模块名
- id:想要显示的视图的id
动作按钮
<record id="action_asset_modify" model="ir.actions.act_window">
<field name="name">Modify Asset</field>
<field name="res_model">asset.modify</field>
<field name="type">ir.actions.act_window</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="view_id" ref="asset_modify_form"/>
<field name="target">new</field>
</record>
<button name="stock.action_asset_modify" type="action"/>
点击按钮,则会跳转到id为“action_asset_modify”的视图页面
type=“workflow”
要有对应的流程对象 ,purchase_confirm 只是一个信号名
<button name="purchase_confirm" states="draft" string="Confirm Order" id="draft_confirm"/>
等效于
<button name="purchase_confirm" states="draft" type="workflow" string="Confirm Order" id="draft_confirm"/>
流程里定义如下:
<record id="purchase_order" model="workflow">
<field name="name">Purchase Order Basic Workflow</field>
<field name="osv">purchase.order</field>
<field name="on_create">True</field>
</record>
节点
<record id="act_draft" model="workflow.activity">
<field name="wkf_id" ref="purchase_order"/>
<field name="flow_start">True</field>
<field name="name">draft</field>
</record>
<record id="act_confirmed" model="workflow.activity">
<field name="wkf_id" ref="purchase_order"/>
<field name="name">confirmed</field>
<field name="split_mode">OR</field>
<field name="kind">function</field>
<field name="action">wkf_confirm_order()</field>
</record>
事务(转换)
<record id="trans_draft_confirmed" model="workflow.transition">
<field name="act_from" ref="act_draft"/>
<field name="act_to" ref="act_confirmed"/>
<field name="signal">purchase_confirm</field>
</record>
看到信号名 purchase_confirm 和 button上的name是一样
对应的节点是有方法wkf_confirm_order()
在模型中有定义
def wkf_confirm_order(self, cr, uid, ids, context=None):
todo = []
for po in self.browse(cr, uid, ids, context=context):
if not any(line.state != 'cancel' for line in po.order_line):
raise osv.except_osv(_('Error!'),_('You cannot confirm a purchase order without any purchase order line.'))
if po.invoice_method == 'picking' and not any([l.product_id and l.product_id.type in ('product', 'consu') and l.state != 'cancel' for l in po.order_line]):
raise osv.except_osv(
_('Error!'),
_("You cannot confirm a purchase order with Invoice Control Method 'Based on incoming shipments' that doesn't contain any stockable item."))
for line in po.order_line:
if line.state=='draft':
todo.append(line.id)
self.pool.get('purchase.order.line').action_confirm(cr, uid, todo, context)
for id in ids:
self.write(cr, uid, [id], {'state' : 'confirmed', 'validator' : uid}, context=context)
return True