1.检查业务逻辑中的错误,终止代码执行,显示错误或警告信息:
raise osv.except_osv(_('Error!'), _('Error Message.'))
示例代码:
#删除当前销售单,需要验证销售单的状态 def unlink(self, cr, uid, ids, context=None): for rec in self.browse(cr, uid, ids, context=context): if rec.state not in ['draft']: raise osv.except_osv(_(u'警告!'),_(u'您不能删除以下状态的销售单 %s .')%(rec.state)) if rec.create_uid.id != uid: raise osv.except_osv(_(u'警告!'),_(u'您不能删除他人创建的单据.')) return super(sale, self).unlink(cr, uid, ids, context)
2.字段的 onchange 事件中返回值,同时返回提示信息:
warning = {
'title': _('Warning!'),
'message' : _('Warning Message.')
}
return {'warning': warning, 'value': value}
示例代码:
def onchange_pricelist_id(self, cr, uid, ids, pricelist_id, order_lines, context=None): context = context or {} if not pricelist_id: return {} value = { 'currency_id': self.pool.get('product.pricelist').browse(cr, uid, pricelist_id, context=context).currency_id.id } if not order_lines: return {'value': value} warning = { 'title': _('Pricelist Warning!'), 'message' : _('If you change the pricelist of this order (and eventually the currency), prices of existing order lines will not be updated.') } return {'warning': warning, 'value': value}
3.视图中 button 按钮点击时显示确认信息:
<button name="cancel_voucher" string="Cancel Voucher" type="object" states="posted" confirm="Are you sure you want to unreconcile this record?"/>
示例代码:
<!-- This general view is used in Invoicing - Journal Entries - Journal Vouchers --> <record model="ir.ui.view" id="view_voucher_form"> <field name="name">account.voucher.form</field> <field name="model">account.voucher</field> <field name="arch" type="xml"> <form string="Accounting Voucher" version="7.0"> <header> <button name="proforma_voucher" string="Post" states="draft" class="oe_highlight"/> <button name="cancel_voucher" string="Cancel Voucher" type="object" states="posted" confirm="Are you sure you want to unreconcile this record?"/> <button name="cancel_voucher" string="Cancel Voucher" states="draft,proforma" /> <button name="action_cancel_draft" type="object" states="cancel" string="Set to Draft"/> <field name="state" widget="statusbar" statusbar_visible="draft,posted" statusbar_colors='{"proforma":"blue"}'/> </header> <sheet string="Accounting Voucher"> <group col="4" colspan="4"> <field name="partner_id" required="1" on_change="onchange_journal_voucher(line_ids, tax_id, amount, partner_id, journal_id, type)"/> <field name="date" on_change="onchange_date(date, currency_id, payment_rate_currency_id, amount, company_id)"/> <field name="journal_id" widget="selection" on_change="onchange_journal_voucher(line_ids, tax_id, amount, partner_id, journal_id, type)"/> <field name="type" required="1"/> <field name="name" colspan="2"/> <field name="company_id" widget="selection" groups="base.group_multi_company"/> <field name="reference"/> <field name="number"/> <field name="currency_id" groups="base.group_multi_currency"/> <field name="account_id" widget="selection" invisible="True"/> <field name="payment_rate_currency_id" invisible="1"/> </group> <notebook colspan="4"> <page string="Voucher Entry"> <field name="line_ids" on_change="onchange_price(line_ids, tax_id, partner_id)" context="{'journal_id':journal_id, 'type':type, 'partner_id':partner_id}"> <tree string="Voucher Items" editable="bottom"> <field name="account_id"/> <field name="name"/> <field name="amount" sum="Total Amount"/> <field name="type"/> <field name="account_analytic_id" groups="analytic.group_analytic_accounting"/> </tree> </field> <group> <field name="narration" nolabel="1" placeholder="Internal Notes"/> <group class="oe_subtotal_footer oe_right" attrs="{'invisible':[('type','in',['payment', 'receipt', False])]}"> <field name="tax_id" on_change="onchange_price(line_ids, tax_id, partner_id)" widget="selection" nolabel="1"/> <field name="tax_amount" nolabel="1"/> <div class="oe_subtotal_footer_separator"> <label for="amount"/> <button type="object" icon="terp-stock_format-scientific" name="compute_tax" class="oe_link oe_edit_only" string="(Update)" attrs="{'invisible': [('state','!=','draft')]}"/> </div> <field name="amount" class="oe_subtotal_footer_separator" nolabel="1"/> </group> </group> </page> <page string="Journal Items" attrs="{'invisible': [('state','!=','posted')]}"> <group col="4"> <field name="period_id"/> <field name="audit"/> </group> <field name="move_ids" readonly="1"> <tree string="Journal Items"> <field name="move_id"/> <field name="ref"/> <field name="date"/> <field name="statement_id"/> <field name="partner_id"/> <field name="account_id"/> <field name="name"/> <field name="debit"/> <field name="credit"/> <field name="state"/> <field name="reconcile_id"/> </tree> </field> </page> </notebook> </sheet> <div class="oe_chatter"> <field name="message_follower_ids" widget="mail_followers"/> <field name="message_ids" widget="mail_thread"/> </div> </form> </field> </record>
原文:http://www.cnblogs.com/cnshen/p/3205405.html