如何通过按钮为选择设置值

How do I set a value on a selection via a button

我需要创建一个按钮,它将状态变量的值定义为 cloturé.

当票是 cloturé 时,它将处于只读状态。

我该怎么做?

门票型号为helpdesk.ticket

view.xml :

<?xml version='1.0' encoding='UTF-8'?>
<odoo>
    <record id="helpdesk_ticket_view_form_inherit_header_modifie" model="ir.ui.view">
        <field name="name">helpdesk.ticket.modifie.header</field>
        <field name="model">helpdesk.ticket</field>
        <field name="inherit_id" ref="helpdesk_fsm.helpdesk_ticket_view_form" />
        <field name="arch" type="xml">
            <xpath expr="//button[@name='action_generate_fsm_task']" position="attributes" >
                <attribute name="string">Planifier tache</attribute>
            </xpath>
        </field>
    </record>
</odoo>

ticket.py :

# -*- coding: utf-8 -*-

from odoo import models, fields, api

class ticket_inherit(models.Model):
    _inherit = "helpdesk.ticket"
    
    state = fields.Selection(['test','annulé','cloturé'],'selection')
    
    #What i want to do
    def cloture_le_ticket(self):
        state = 'cloturé'
    

你可以直接使用 write().

state = fields.Selection(string="State", selection=[
    ('test', 'Test),
    ('annulé', 'Annulé'),
    ('cloturé', 'Cloturé')
])

def cloture_le_ticket(self):
    self.write({
        'state': 'cloturé'
    })

强制票为read-only:

def write(self, vals):
    for record in self:
        if record.state == 'cloturé':
            raise exceptions.UserError("Sorry but the ticket is read only")
    return super(HelpdeskTicket, self).write(vals)