odoo12 修行基礎篇之 記錄批處理 (七)


客戶需求總是不斷啊...

批處理我們得先有個按鈕啊,加吧,加的過程中就加上了批量處理方式了。

在加按鈕之前還要說下odoo中常用的三類模型,因為,一種我們沒見過的模型即將登場...

基本模型:model.Model,儲存數據記錄的模型,不會定期刪除

瞬態模型:model.TransientModel,瞬態模型,數據會被定期刪除,常用於與用戶的臨時交互

抽象模型:model.AbstractModel,抽象模型,不會存數據(還得研究研究,這里我嘗試用了以下,一直報錯,沒咋搞懂,希望多交流)

這里我們用到了瞬態模型

1、創建my_customer_complain_transient.py文件,再init.py中引入。

方法內容先不管。

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

from odoo import models, api
from odoo.exceptions import UserError


class MyCustomerComplainTransient(models.TransientModel):
    _name = 'my.customer.complain.transient'
    _description = '批量確認'

    # 預處理方法
    @api.model
    def default_get(self, fields_list):
        records = self._get_all_complains(self._context['active_ids'])
        records_undraft_names = records.filtered(lambda r: r.state != 'draft').mapped('name')
        if records_undraft_names:
            raise UserError('下列訂單狀態不符: \n%s\n請選擇未處理的清單' % ','.join(records_undraft_names))
        return super().default_get(fields_list)

    def action_confirm(self):
        records = self._get_all_complains(self._context['active_ids'])
        records.write({'state': 'done'})

    def _get_all_complains(self, record_ids):
        return self.env['my.customer.complain'].search([('id', 'in', record_ids)])

簡單說下:default_get是預處理方法,會在form頁面彈出前調用。action_confirm是在form頁面彈出后點擊確認時調用。

self._context['active_ids']

上述代碼可以獲取選擇記錄的id列表,根據這個列表可以獲取記錄。

2、創建my_customer_complain_transient.xml,在__manifest__.py中引入。

<?xml version="1.0" encoding="UTF-8"?>
<odoo>
    <record id="view_my_customer_complain_transient" model="ir.ui.view">
        <field name="name">view.my.customer.complain.transient</field>
        <field name="model">my.customer.complain.transient</field>
        <field name="arch" type="xml">
            <form string="提示">
                <group>
                    <p>
                        即將進行批量確認,是否繼續?
                    </p>
                </group>
                <footer>
                    <button name="action_confirm" string="確認" type="object" class="btn-primary"/>
                    <button string="取消" class="btn-secondary" special="cancel" />
                </footer>
            </form>
        </field>
    </record>
</odoo>

3、在my_customer_complain.xml中增加如下內容,把之前context分組的條件去掉

<!-- 批量確認 -->
<act_window id="action_my_customer_complain_multi_confirm"
            name="批量確認"
            src_model="my.customer.complain"
            res_model="my.customer.complain.transient"
            view_mode="form"
            key2="client_action_multi"/>

id: 唯一標記

src_model: 批量確認按鈕添加的模型

res_model: 調用的模型

view_mode: 彈出框的類型,默認是列表,這里填form,為了和用戶更好的交互

key2: 固定寫法,不加沒有按鈕

按照上述定義完后,升級下,按鈕就會出現在頁面中。點擊通過預處理方法判斷后,就會調用預先定義的form進行展示。

有其它狀態的記錄時會提示:

沒有狀態不符的記錄,就彈出form:

確認就批量完成記錄了。

這樣列表基本上就搞的差不多了,后面我們搞下kanban吧。

 

項目git地址:https://github.com/SamNicole1809/odoo12_my_pro,帶目錄


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM