【odoo15】在odoo中覆盖Unlink方法,显示删除时的验证信息

有些表不能删除,就比处于完成状态下的订单表,所以为了防止误删,可以重写删除方法,加以保护。如图所示:

代码:py代码:(当state状态为done时不可删除)

    def unlink(self):
        print("删除成功")
        if self.state == 'done':
            raise ValidationError("你不能删除表单号为%s,因为此表单已完成done" % self.serial_number)
        return super(mysale, self).unlink()

要在上方引入这个ValidationError 异常类

from odoo.exceptions import ValidationError

完整py代码:

from odoo import fields, models, api, _
from odoo.exceptions import ValidationError


class mysale(models.Model):
    _name = "mysale.mysale"
    _description = "mysale模块"
    _order = "serial_number desc"
    _inherit = ['mail.thread', 'mail.activity.mixin']
    serial_number = fields.Char(string="序列号", copy=False, readonly=True,
                                default=lambda self: _('NEW'))
    name = fields.Char(tracking=True, required=True)
    field = fields.Char()
    note = fields.Char(tracking=True)
    gender = fields.Selection([('male', '男的'), ('female', '女的'), ('other', '其它')],
                              required=True, default='other')
    mynote = fields.Text(string="备注便签")
    state = fields.Selection(
        [('draft', '草稿'), ('confirm', '确认'),
         ('done', '完成'), ('cancel', '取消')], string="状态", default='draft')
    # product.template
    product_id = fields.Many2one('product.template', string="产品")

    date_test1 = fields.Date(string="日期")
    date_test2 = fields.Datetime(string="时间")
    # value3 = fields.Integer()

    image = fields.Binary(string="上传图片")
    first_notebook = fields.Text(string="记事本")
    test_onetwomany = fields.One2many('mysale.mysale', 'product_id', string="测试onetwomany")

    # 在one2many中   (关联表的表名称    --   many2one的字段    --  string值)

    O2M_line_ids = fields.One2many('mysale.lines', 'M2O_id', string="字段o2m")

    def action_draft(self):
        self.state = 'draft'

    def acton_confirm(self):
        self.state = 'confirm'
        # print(self.name)
        # # print(self.name.id)
        # print(self.id)

    def action_done(self):
        self.state = 'done'

    def action_cancel(self):
        self.state = 'cancel'

    # 模型修饰器
    @api.model
    def create(self, vals):
        # 覆盖create方法
        if vals.get('serial_number', _('New')) == _('New'):
            vals['serial_number'] = self.env['ir.sequence'].next_by_code('mysale.mysale') or _('New')
        res = super(mysale, self).create(vals)
        return res

    def unlink(self):
        print("删除成功")
        if self.state == 'done':
            raise ValidationError("你不能删除表单号为%s,因为此表单已完成done" % self.serial_number)
        return super(mysale, self).unlink()

class MySaleLines(models.Model):
    _name = "mysale.lines"
    _description = "mysale-lines模块"
    test_1 = fields.Char(string="测试字段1", required=True)
    test_2 = fields.Char(string="测试字段2", required=True)
    test_3 = fields.Integer(string="测试字段3", required=True)
    M2O_id = fields.Many2one('mysale.mysale', string="M2TO")

完整xml代码:

<?xml version="1.0" encoding="utf-8"?>

<odoo>
    <record model="ir.actions.act_window" id="mysale_action_window">
        <field name="name">mysale 视图</field>
        <field name="res_model">mysale.mysale</field>
        <field name="view_mode">tree,form,kanban</field>
    </record>

    <record model="ir.ui.view" id="mysale_tree">
        <field name="name">mysale tree</field>
        <field name="model">mysale.mysale</field>
        <field name="arch" type="xml">
            <tree multi_edit="1">
                <header>
                    <button name="action_done" string="完成" class="btn-primary" type="object"/>
                </header>
                <field name="serial_number" readonly="1"/>
                <field name="name"/>
                <field name="field"/>
                <field name="gender"/>
                <field name="note"/>
                <field name="state"/>
                <field name="product_id"/>
            </tree>
        </field>
    </record>


    <record model="ir.ui.view" id="mysale_form">
        <field name="name">mysale form</field>
        <field name="model">mysale.mysale</field>
        <field name="arch" type="xml">
            <form>
                <header>
                    <button id="button_draft" name="action_draft" string="草稿" class="btn-primary"
                            states="cancel" type="object"/>
                    <button id="button_confirm" name="acton_confirm" string="确认" class="btn-primary"
                            states="draft,cancel" type="object" confirm="确认选择完成?"/>
                    <button id="button_done" name="action_done" string="完成" class="btn-primary"
                            states="cancel,draft,confirm" type="object"/>
                    <button id="button_cancel" name="action_cancel" string="取消"
                            states="draft,done,confirm" type="object"/>
                    <field name="state" widget="statusbar" options="{'clickable':'1'}"
                           statusbar_visible="draft,confirm,done"/>
                </header>
                <sheet>
                    <field name="image" widget="image" class="oe_avatar"/>
                    <div class="oe_title">
                        <h1>
                            <field name="serial_number" readonly="1"/>
                        </h1>
                    </div>
                    <group class="top">
                        <group class="left">
                            <field name="name"/>
                            <field name="field"/>
                            <field name="note"/>
                            <field name="gender"/>
                            <field name="mynote"/>
                            <field name="product_id"/>
                            <field name="date_test1"/>
                            <field name="date_test2"/>
                        </group>
                    </group>
                    <notebook>
                        <page string="my notebook" name="mysale_notebook">
                            <group>
                                <field name="first_notebook"/>
                            </group>
                        </page>

                          <page string="my notebook" name="mysale_notebook">
                            <group>
                                <field name="test_onetwomany"/>
                            </group>
                        </page>

                        <page string="my notebook" name="mysale_notebook">
                            <field name="O2M_line_ids" readonly="1">
                                <tree editable="bottom" create="1" delete="0" edit="1">
                                    <field name="test_1" optional="show"/>
                                    <field name="test_2" optional="show"/>
                                    <field name="test_3" optional="hide"/>
                                    <field name="M2O_id" optional="show"/>
                                </tree>
                                <form>
                                    <group>
                                        <group>
                                        <field name="test_1"/>
                                        <field name="test_2"/>
                                        <field name="test_3"/>
                                        <field name="M2O_id"/>
                                        </group>
                                    </group>
                                </form>
                            </field>
                        </page>
                    </notebook>
                    <div class="oe_chatter">
                        <field name="message_follower_ids"/>
                        <field name="activity_ids"/>
                        <field name="message_ids"/>
                    </div>
                </sheet>
            </form>
        </field>
    </record>


    <!--    写一个search 搜索条件 -->
    <record model="ir.ui.view" id="mysale_search">
        <field name="name">mysale_search</field>
        <field name="model">mysale.mysale</field>
        <field name="arch" type="xml">
            <search string="search_field">
                <field name="name"/>
                <field name="field"/>
                <field name="note"/>
                <separator/>
                <filter string="男生" name="male" domain="[('gender','=','male')]"/>
                <filter string="女生" name="female" domain="[('gender','=','female')]"/>
                <group expand="1" string="性别组">
                    <filter string="性别组" name="gender" context="{'group_by':'gender'}"/>
                    <filter string="姓名组" name="name" context="{'group_by':'name'}"/>
                </group>
            </search>
        </field>
    </record>


    <!--    自定义kanban视图    -->
    <record id="mysale_kanban" model="ir.ui.view">
        <field name="name">mysale kanban</field>
        <field name="model">mysale.mysale</field>
        <field name="arch" type="xml">
            <kanban>
                <field name="name"/>
                <field name="field"/>
                <field name="gender"/>
                <templates>
                    <t t-name="kanban-box">
                        <div class="oe_kanban_global_click">
                            <div class="o_kanban_image">
                                <img t-att-src="kanban_image('im_livechat.channel', 'image_128')"
                                     class="img-fluid" alt="Channel"/>
                            </div>
                            <ul>
                                <li>姓名:
                                    <field name="name"/>
                                </li>
                                <li>领域:
                                    <field name="field"/>
                                </li>
                                <li>性别:
                                    <field name="gender"/>
                                </li>
                            </ul>
                        </div>
                    </t>
                </templates>
            </kanban>
        </field>
    </record>

    <menuitem id="mysale_menu_root"
              name="mysale菜单"
              action="mysale_action_window"/>

    <menuitem id="menu_1_list"
              name="mysale"
              parent="mysale_menu_root"
              action="mysale_action_window"
              sequence="10"/>

    <menuitem id="menu_1_list1"
              name="mysale"
              parent="menu_1_list"
              action="mysale_action_window"
              sequence="10"/>


</odoo>

重启刷新网页即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值