Python 如何使树视图和表单视图中的特定记录在odoo中不可编辑?

Python 如何使树视图和表单视图中的特定记录在odoo中不可编辑?,python,odoo,Python,Odoo,我有一个带有“state”(selection)属性的实体账单(西班牙语factura)。是否可以使“账单”中的所有记录(state='pendiente'或state='pagad')为只读?这意味着,当用户在树状视图上单击特定账单时,他无法编辑任何“账单”字段。这是我的密码 class PlanificacionFactura(models.Model): _name = 'utepda_planificacion.factura' _rec_name = 'numero'

我有一个带有“state”(selection)属性的实体账单(西班牙语factura)。是否可以使“账单”中的所有记录(state='pendiente'或state='pagad')为只读?这意味着,当用户在树状视图上单击特定账单时,他无法编辑任何“账单”字段。这是我的密码

class PlanificacionFactura(models.Model):
    _name = 'utepda_planificacion.factura'
    _rec_name = 'numero'
    _description = 'Factura'
    _inherit = ['mail.thread', 'mail.activity.mixin']

    fecha = fields.Date(string='Fecha')
    monto_total = fields.Monetary(string='Monto a pagar', currency_field='currency_id')
    pago_acumulado = fields.Monetary(compute='_compute_pago_acumulado', string='Pago Acumulado' ,currency_field='currency_id')
    currency_id = fields.Many2one('res.currency', string='Moneda', required=True, domain=[('name', 'in', ('USD', 'DOP'))] , default=lambda self: self.env.ref("base.DOP"))
    pago_pendiente = fields.Monetary(compute='_compute_pago_pendiente', string='Pendiente de pago', currency_field='currency_id')

    state = fields.Selection([
        ('creado', 'Creada'),
        ('pendiente','Pagada parcialmente'),
        ('pagado','Pagada')
    ], string='Estado', default='pagado', compute='_compute_state' )

    @api.depends('pago_acumulado','monto_total')
    def _compute_state(self):
        for record in self:
            if record.pago_acumulado > 0 and record.pago_acumulado < record.monto_total:
                record.state='pendiente'
            elif record.pago_acumulado == record.monto_total:
                record.state = 'pagado'
            else:
                record.state='creado'
    @api.model
    def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
        res = super(PlanificacionFactura, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu)
        if view_type == 'form':
            doc = etree.XML(res['arch'])
            for node in doc.xpath("//field[@name='fecha']"):
                node.set('options', "{'datepicker': {'maxDate': '%s'}}" % fields.Date.today().strftime(DEFAULT_SERVER_DATE_FORMAT))

            if 'params' in self.env.context and 'id' in self.env.context['params']:
                values = self.search_read([('id', '=', self.env.context['params']['id'])], fields=['state'])
                if values[0]['state'] == 'pagado':
                    # Disable edit mode on form view based on `state` field
                    for node in doc.xpath("//form"):
                        node.set('edit', '0')

            res['arch'] = etree.tostring(doc)
        return res
类平面构造(models.Model):
_名称='utepda_Planificationacion.factura'
_记录名称='numero'
_description=‘Factura’
_inherit=['mail.thread','mail.activity.mixin']
fecha=fields.Date(string='fecha')
monto\u total=fields.Monetary(字符串='monto a pagar',currency\u field='currency\u id')
pago_acumulado=fields.Monetary(compute=''u compute_pago_acumulado',string='pago acumulado',currency\u field='currency\u id')
currency_id=fields.manyOne('res.currency',string='Moneda',required=True,domain=[('name','in',('USD','DOP'))],default=lambda self:self.env.ref(“base.DOP”))
pago_pendiente=fields.Monetary(compute=''u compute_pago_pendiente',string='pendiente de pago',currency_field='currency_id')
状态=字段。选择([
(“creado”,“Creada”),
(‘pendiente’,‘Pagada parcialmente’),
(“帕加多”,“帕加达”)
],string='Estado',default='pagado',compute=''u compute\u state')
@api.depends('pago_acumulado','monto_total')
定义计算状态(自身):
请自行记录:
如果record.pago_acumulado>0且record.pago_acumulado
我使状态在状态栏中可见

    <header>
        <field name="state" widget="statusbar"/>
    </header>

您可以覆盖
FormRenderer
,以便在加载表单之后检查
状态
字段

您可以将条件指定为域,并使用类使用当前记录字段值计算它

var FormRenderer = require('web.FormRenderer');
var Domain = require("web.Domain");
FormRenderer.include({

    autofocus: function () {
        this.show_hide_edit_button();
        return this._super();
    },

    show_hide_edit_button : function () {
        if( 'disable_edit_mode' in this.arch.attrs && this.arch.attrs.disable_edit_mode) {
            var domain = Domain.prototype.stringToArray(this.arch.attrs.disable_edit_mode)
            var button = $(".o_form_button_edit");
            if (button) {
                var hide_edit = new Domain(domain).compute(this.state.data);
                // If you just need to disable edit button
                // button.prop('disabled', hide_edit);
                
                // button is shown if the parameter is true and hidden if it is false
                button.toggle(!hide_edit);
            }
        }
    },

});
使用
disable\u edit\u mode
属性设置域条件:

<form disable_edit_mode="[('state', '=', 'pagado')]">  


请参阅将上述代码添加到您的模块。

您可以在加载表单后覆盖
FormRenderer
以检查
状态
字段

您可以将条件指定为域,并使用类使用当前记录字段值计算它

var FormRenderer = require('web.FormRenderer');
var Domain = require("web.Domain");
FormRenderer.include({

    autofocus: function () {
        this.show_hide_edit_button();
        return this._super();
    },

    show_hide_edit_button : function () {
        if( 'disable_edit_mode' in this.arch.attrs && this.arch.attrs.disable_edit_mode) {
            var domain = Domain.prototype.stringToArray(this.arch.attrs.disable_edit_mode)
            var button = $(".o_form_button_edit");
            if (button) {
                var hide_edit = new Domain(domain).compute(this.state.data);
                // If you just need to disable edit button
                // button.prop('disabled', hide_edit);
                
                // button is shown if the parameter is true and hidden if it is false
                button.toggle(!hide_edit);
            }
        }
    },

});
使用
disable\u edit\u mode
属性设置域条件:

<form disable_edit_mode="[('state', '=', 'pagado')]">  


请参阅将上述代码添加到您的模块。

您是否尝试使用
attrs=“{'readonly':[('paid','=',True)]}”
?我无法将该atribute应用于整个表单。其思想是在只读模式下打开表单。如果表单是在只读模式下打开的,则必须单击“编辑”按钮以使字段可编辑。你的意思是使表单不可编辑,隐藏编辑按钮吗?请阅读,我建议你从开始。你是否尝试使用
attrs=“{'readonly':[('paid','=',True)]}”
?我无法将该atribute应用于整个表单。其思想是在只读模式下打开表单。如果表单是在只读模式下打开的,则必须单击“编辑”按钮以使字段可编辑。你的意思是使表单不可编辑,隐藏编辑按钮吗?请阅读,我建议你从开始。