我想基于布尔字段隐藏one2many树视图中的字段
如果选中了布尔字段,则在下面的树视图中显示字段总数。
树视图属于class'account.invoice.line',代码如下(布尔字段):
class account_invoice_line(models.Model):
_name = "account.invoice.line"
_description = "Invoice Line"
_order = "invoice_id,sequence,id"
name = fields.Text(string='Description')
tot_qty_bool = fields.Boolean(string='Total Quantity', default=False)
tot_qty = fields.Float(string='Total Quantity')
origin = fields.Char(string='Source Document',
help="Reference of the document that produced this invoice.")
sequence = fields.Integer(string='Sequence', default=10,
help="Gives the sequence of this line when displaying the
invoice.")
invoice_id = fields.Many2one('account.invoice', string='Invoice
Reference',
ondelete='cascade', index=True)
uos_id = fields.Many2one('product.uom', string='Unit of Measure',
ondelete='set null', index=True)
product_id = fields.Many2one('product.product', string='Product',
ondelete='set null', index=True)
account_id = fields.Many2one('account.account', string='Account',
domain=[('type', 'not in', ['view', 'closed'])],
default=_default_account,
help="The income or expense account related to the selected
product.")
price_unit = fields.Float(string='Unit Price', required=True,
digits= dp.get_precision('Product Price'),
default=_default_price_unit)
price_subtotal = fields.Float(string='Amount', digits=
dp.get_precision('Account'),
store=True, readonly=True, compute='_compute_price')
quantity = fields.Float(string='Actual Quantity', digits=
dp.get_precision('Product Unit of Measure'),
required=True, default=1)
discount = fields.Float(string='Discount (%)', digits=
dp.get_precision('Discount'),
default=0.0)
invoice_line_tax_id = fields.Many2many('account.tax',
'account_invoice_line_tax', 'invoice_line_id', 'tax_id',
string='Taxes', domain=[('parent_id', '=', False)])
account_analytic_id = fields.Many2one('account.analytic.account',
string='Analytic Account')
company_id = fields.Many2one('res.company', string='Company',
related='invoice_id.company_id', store=True, readonly=True)
partner_id = fields.Many2one('res.partner', string='Partner',
related='invoice_id.partner_id', store=True, readonly=True)
和代码部分我在fields_view_get中尝试了类似的东西(没有成功)
def fields_view_get(self, view_id=None, view_type='form',
toolbar=False, submenu=False):
res = super(account_invoice_line, self).fields_view_get(
view_id=view_id, view_type=view_type, toolbar=toolbar,
submenu=submenu)
if self._context.get('tot_qty_bool'):
doc1 = etree.XML(res['arch'])
for node in doc.xpath("//field[@name='tot_qty']"):
if self._context['tot_qty_bool'] in ('False'):
node.set('invisible', '1')
else:
node.set('invisible', '0')
res['arch'] = etree.tostring(doc)
return res
请指导我这样做的正确方法,或者让我知道是否有其他正确的方法可以实现相同目的。真的是一个很大的帮助。
答案 0 :(得分:2)
fields_view_get
。因此,只有在呈现account.invoice.line
视图时才能执行您尝试执行的操作。
尝试继承invoice_form
视图并使用attrs
隐藏tot_qty
字段。 (您需要在树视图中添加tot_qty_bool
字段)。
<record id="invoice_form_inherit" model="ir.ui.view">
<field name="name">invoice.form.inherit</field>
<field name="model">account.invoice</field>
<field name="inherit_id" ref="account.invoice_form"/>
<field name="arch" type="xml">
<xpath expr="//tree/field[@name='name']" position="after">
<field name='tot_qty_bool' invisible="True"/>
<field name='tot_qty' attrs="{'invisible':[('tot_qty_bool', '=', False)]}"/>
</xpath>
</field>
</record>
答案 1 :(得分:1)
正如@macdelacruz已在评论中提及,请在树状视图定义中使用attrs
和invisible
。
我在Odoo V8 runbot上测试过类似的例子。
从原始时间表表单视图:
<field name="name" />
只需将其更改为:
<field name="name" attrs="{'invisible': [('unit_amount','=',0)]}"/>
保存,重新加载并尝试使用它。它正在工作。