(48) odoo的button用法
button 应用的比较多,这里来汇总一下
按钮<button>,支持的属性
icon 可用的icon在 addons/web/static/src/img/icons
string 按钮的显示文字
type 值可以是 workflow, object action 默认是 workflow
name 就是要触发的方法标识
args 传递方法的参数
content 上下文
confirm 针对对话框的确认
special="cancel" 用于向导
states 可见的状态
classname 加载的类名(常用 oe_highlight)
<footer>
<button string="Apply" name="action_apply" type="object" class="oe_highlight"/>
or
<button string="Cancel" class="oe_link" special="cancel" />
</footer>
* <button string="Cancel" class="oe_link" special="cancel" />
这是普通的取消
* <button string="Apply" name="action_apply" type="object" class="oe_highlight"/>
这是 type 为 object 这是的交处理的业务
要在对应的模型中定义方法:
def action_apply(self, cr, uid, ids, context=None):
wizard = self.browse(cr, uid, ids[0], context)
portal_user_ids = [user.id for user in wizard.user_ids]
self.pool.get('portal.wizard.user').action_apply(cr, uid, portal_user_ids, context)
return {'type': 'ir.actions.act_window_close'}
* <button string="? Request Procurement" name="%(stock.act_make_procurement)d" type="action" class="oe_link"/>
这个引用一个action,调用的是 stock 模块中的 id为 act_make_procurement 的 act_window
如下:
<record model="ir.actions.act_window" id="act_make_procurement">
<field name="name">Procurement Request</field>
<field name="res_model">make.procurement</field>
<field name="src_model">product.product</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>
* <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"/>
是等效的
这是流程的,这时就要有对应的流程对象 purchase_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
对比了一下,还是type 为 workflow 复杂一些,小心看,还是不难的