Python “如何隐藏按钮”;“创建发票”;但基于条件?(奥多11)

Python “如何隐藏按钮”;“创建发票”;但基于条件?(奥多11),python,xml,odoo,Python,Xml,Odoo,我真的被阻止了,我想隐藏“创建发票”按钮,但根据一个条件,我的条件是,如果订单行有服务,该按钮是隐藏的。我创建了一个字段和一个函数,但最后总是出现一个错误,即该字段在模型中不存在,这是我的代码: from odoo import api, fields, models,_ class SaleOrder(models.Model): _inherit = 'sale.order' hide_invoice = fields.Boolean(compute="_hide_bu

我真的被阻止了,我想隐藏“创建发票”按钮,但根据一个条件,我的条件是,如果订单行有服务,该按钮是隐藏的。我创建了一个字段和一个函数,但最后总是出现一个错误,即该字段在模型中不存在,这是我的代码:

from odoo import api, fields, models,_


class SaleOrder(models.Model):
    _inherit = 'sale.order'

    hide_invoice = fields.Boolean(compute="_hide_button_invoice", string="",)


    @api.multi
    @api.depends('tasks_count')
    def _hide_button_invoice(self):
        for order in self:
            if order.tasks_count > 0:
                order.hide_invoice = True
            elif order.tasks_count == 0:
                order.hide_invoice = False 
错误:

<odoo>
<record id="button_invoice_view_form" model="ir.ui.view">
        <field name="name">sale.order.button.create.form</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='partner_id']" position="before">
                <field name ="hide_invoice"/>
            </xpath>
        </field>
</record>
</odoo>
<record id="sale_order_view_form" model="ir.ui.view">
    <field name="name">sale.order.form</field>
    <field name="model">sale.order</field>
    <field name="inherit_id" ref="sale.view_order_form"/>
    <field name="arch" type="xml">
        <xpath expr="//group[@name='sale_pay']/field[@name='invoice_status']" position="attributes">
            <attribute name="invisible" eval="False"/>
        </xpath>
        <xpath expr="//button[@name='action_quotation_send']" position="before">
            <button name="%(sale.action_view_sale_advance_payment_inv)d" string="Create Invoice"
                type="action" class="btn-primary"
                attrs="{'invisible': [('invoice_status', '!=', 'to invoice')]}"/>
            <button name="%(sale.action_view_sale_advance_payment_inv)d" string="Create Invoice"
                type="action" context="{'default_advance_payment_method': 'percentage'}"
                attrs="{'invisible': ['|','|',('hide_invoice', '=', True),('invoice_status', '!=', 'no'), ('state', '!=', 'sale')]}"/>
        </xpath>
    </field>
</record>
属性中使用的字段“隐藏发票”必须出现在视图中,但缺少该字段

我的字段和函数(Python):

from odoo import api, fields, models,_


class SaleOrder(models.Model):
    _inherit = 'sale.order'

    hide_invoice = fields.Boolean(compute="_hide_button_invoice", string="",)


    @api.multi
    @api.depends('tasks_count')
    def _hide_button_invoice(self):
        for order in self:
            if order.tasks_count > 0:
                order.hide_invoice = True
            elif order.tasks_count == 0:
                order.hide_invoice = False 
我的XML(我在表单上看到了它的工作原理):

<odoo>
<record id="button_invoice_view_form" model="ir.ui.view">
        <field name="name">sale.order.button.create.form</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='partner_id']" position="before">
                <field name ="hide_invoice"/>
            </xpath>
        </field>
</record>
</odoo>
<record id="sale_order_view_form" model="ir.ui.view">
    <field name="name">sale.order.form</field>
    <field name="model">sale.order</field>
    <field name="inherit_id" ref="sale.view_order_form"/>
    <field name="arch" type="xml">
        <xpath expr="//group[@name='sale_pay']/field[@name='invoice_status']" position="attributes">
            <attribute name="invisible" eval="False"/>
        </xpath>
        <xpath expr="//button[@name='action_quotation_send']" position="before">
            <button name="%(sale.action_view_sale_advance_payment_inv)d" string="Create Invoice"
                type="action" class="btn-primary"
                attrs="{'invisible': [('invoice_status', '!=', 'to invoice')]}"/>
            <button name="%(sale.action_view_sale_advance_payment_inv)d" string="Create Invoice"
                type="action" context="{'default_advance_payment_method': 'percentage'}"
                attrs="{'invisible': ['|','|',('hide_invoice', '=', True),('invoice_status', '!=', 'no'), ('state', '!=', 'sale')]}"/>
        </xpath>
    </field>
</record>

sale.order.button.create.form
销售订单
按下按钮,然后我想隐藏它:

<odoo>
<record id="button_invoice_view_form" model="ir.ui.view">
        <field name="name">sale.order.button.create.form</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='partner_id']" position="before">
                <field name ="hide_invoice"/>
            </xpath>
        </field>
</record>
</odoo>
<record id="sale_order_view_form" model="ir.ui.view">
    <field name="name">sale.order.form</field>
    <field name="model">sale.order</field>
    <field name="inherit_id" ref="sale.view_order_form"/>
    <field name="arch" type="xml">
        <xpath expr="//group[@name='sale_pay']/field[@name='invoice_status']" position="attributes">
            <attribute name="invisible" eval="False"/>
        </xpath>
        <xpath expr="//button[@name='action_quotation_send']" position="before">
            <button name="%(sale.action_view_sale_advance_payment_inv)d" string="Create Invoice"
                type="action" class="btn-primary"
                attrs="{'invisible': [('invoice_status', '!=', 'to invoice')]}"/>
            <button name="%(sale.action_view_sale_advance_payment_inv)d" string="Create Invoice"
                type="action" context="{'default_advance_payment_method': 'percentage'}"
                attrs="{'invisible': ['|','|',('hide_invoice', '=', True),('invoice_status', '!=', 'no'), ('state', '!=', 'sale')]}"/>
        </xpath>
    </field>
</record>

销售单
销售订单

您必须在标题级别显示字段才能处理这些问题

在声明按钮之前,请执行以下代码

<field name='hide_invoice' invisible='1'/>

并在partner_id字段之前将其删除

编辑

您能否尝试使用以下xml代码:

<record id="sale_order_view_form" model="ir.ui.view">
    <field name="name">sale.order.form</field>
    <field name="model">sale.order</field>
    <field name="inherit_id" ref="sale.view_order_form"/>
    <field name="arch" type="xml">
        <xpath expr="//group[@name='sale_pay']/field[@name='invoice_status']" position="attributes">
            <attribute name="invisible" eval="False"/>
        </xpath>
        <xpath expr="//button[@name='action_quotation_send']" position="before">
            <button name="%(sale.action_view_sale_advance_payment_inv)d" string="Create Invoice"
                type="action" class="btn-primary"
                attrs="{'invisible': [('invoice_status', '!=', 'to invoice')]}"/>
            <field name="hide_invoice" invisible="1"/>
            <button name="%(sale.action_view_sale_advance_payment_inv)d" string="Create Invoice"
                type="action" context="{'default_advance_payment_method': 'percentage'}"
                attrs="{'invisible': ['|','|',('hide_invoice', '=', True),('invoice_status', '!=', 'no'), ('state', '!=', 'sale')]}"/>
        </xpath>
    </field>
</record>

销售单
销售订单

在提出简单问题之前,请阅读文档或浏览教程网站。您确定必须区分表单视图中的页眉、正文和页脚吗?@CZoellner这将取决于我们使用该字段的位置。简而言之,字段应该在使用前声明,就像在attrs中一样。页眉、正文和页脚之间没有关系。我有一个模块,将隐藏按钮的第一次和之后,它是可见的。请纠正我。我试过了,但不起作用,我配置:
@NabilTaleb您可以在您的问题中共享完整更新的xml文件代码吗。同样,现在是:>字段“隐藏发票”不存在