向 odoo many2many 字段添加额外的字段

Add extra fields to odoo many2many field

我正在尝试在继承自 hr.employee 的 hr.employee.This 模块中创建一个精细的分配功能。过程是:

  1. 单击打开向导的精细分配菜单。该向导有一个名为 employee_ids 的多对多关系字段,与 hr.employee 相关。这应该使您能够 select 分批员工并一起分配罚款。

2.There 是一个名称为 fine_amount 的字段,为每行中的员工分配个人罚款,然后通过单击按钮分配罚款。

我的问题是,如何使用 fine_amount 字段扩展这个 many2many 字段?

如果我用这个字段扩展 hr.employee 表单,它不在 many2many 行的 selected 字段中。

我的代码如下所示

class FineAllocation(models.TransientModel):
_name = 'fine.allocation'
_description = 'Fine Allocation'

@api.model
def action_allocate_fine(self):
    pass

employee_ids = fields.Many2many('hr.employee','hr_employee_group_rel', 'deductions_id' 'Employees')
fine_amount = fields.Float(string='Fine Amount')

这是向导记录查看

<record id="employee_fine_allocation_form" model="ir.ui.view">
        <field name="name">Employee Fine Allocation Form</field>
        <field name="model">fine.allocation</field>
        <field name="arch" type="xml">
            <form string="Employee Fine Allocation">
                <group>
                    <span colspan="4" nolabel="1">This wizard will allocate fines for all selected employee(s) equalling the input fine amount.</span>
                </group>

                <group colspan="4">
                    <separator string="Employees" colspan="4" />
                    <newline />
                    <field name="employee_ids" nolabel="1" />
                </group>

                <footer>
                    <button name="action_allocate_fine" string="Allocate Fines" type="object" class="btn-primary" />
                    <button string="Cancel" class="btn-secondary" special="cancel" />
                </footer>
            </form>
        </field>
    </record>

默认情况下,many2many 字段显示默认视图。

When a view is requested by (model, type), the view matching the model and the type, with the lowest priority will be returned (it is the default view).

要在 many2many 列表中显示该字段,您可以扩展默认视图或定义您的视图,请尝试以下操作之一:

  1. 扩展默认树视图hr.view_employee_tree:

     <record id="view_employee_tree" model="ir.ui.view">
         <field name="name">hr.employee.tree</field>
         <field name="model">hr.employee</field>
         <field name="inherit_id" ref="hr.view_employee_tree"/>
         <field name="arch" type="xml">
             <tree>
                 <field name="fine_amount"/>
             </tree>
         </field>
     </record>
    
  2. 定义一个新的树视图并强制 many2many 字段显示它:

     <record id="view_employee_tree_allocate_fine" model="ir.ui.view">
     <field name="name">hr.employee.tree.allocate.fine</field>
     <field name="model">hr.employee</field>
     <field name="arch" type="xml">
         <tree string="Employees" decoration-bf="message_needaction==True">
             <field name="name"/>
             <field name="work_phone" class="o_force_ltr"/>
             <field name="work_email"/>
             <field name="company_id" groups="base.group_multi_company"/>
             <field name="department_id"/>
             <field name="job_id"/>
             <field name="parent_id"/>
             <field name="coach_id" invisible="1"/>
             <field name="message_needaction" invisible="1"/>
    
             <field name="fine_amount"/>
         </tree>
     </field>
     </record>
    

    要强制 many2many 字段显示特定视图,请在上下文中传递 tree_view_ref

    <field name="employee_ids" nolabel="1" context="{'tree_view_ref': 'module_name.view_employee_tree_allocate_fine'}"/>
    
  3. 就地定义树视图:

     <field name="employee_ids">
         <tree editable="bottom">
             <field name="name"/>
             <field name="work_phone" class="o_force_ltr"/>
             <field name="work_email"/>
             <field name="company_id" groups="base.group_multi_company"/>
             <field name="department_id"/>
             <field name="job_id"/>
             <field name="parent_id"/>
             <field name="coach_id" invisible="1"/>
             <field name="message_needaction" invisible="1"/>
    
             <field name="fine_amount"/>
         </tree>
     </field>
    

编辑:

在适当位置定义树视图时,将 editable 选项设置为 bottom

编辑2:

定义如下:

employee_ids = fields.Many2many('hr.employee', 'hr_employee_rel', 'payslip_id', 'Employees')

Odoo 将尝试创建记录并将 column2 的值(在上面的示例中未明确传递)设置为 Employees,这将引发以下错误:

the “employees” column of the “hr_employee_rel” relationship does not exist
LINE 2: ...   INSERT INTO hr_employee_rel (payslip_id, Employees)...
如果将

employee_idhr_payroll 向导模型中的 employee_ids 进行比较,则缺少 employee_id

在你的问题中,你可以看到 deductions_idEmployees 之间没有逗号,Python 将连接两个字符串,你将以 column1 结尾设置为 deductions_idEmployees。 Odoo 应该引发以下错误:

psycopg2.errors.UndefinedColumn: ERROR: the column « deductions_idemployees » of the relation « hr_employee_group_rel » does not exist
LINE 2: ... INSERT INTO hr_employee_group_rel (deductions ...

如果将 Employees 作为字符串属性传递,则可以避免该错误,第二列(column2 属性)应该自动生成,因为它是可选的。你可以试试下面的定义:

employee_ids = fields.Many2many('hr.employee', 'hr_employee_fine_allocation_rel', 'payslip_id', string='Employees')

属性 relationcolumn1column2 是可选的。如果未给出,名称将自动从模型名称生成。

如果不需要显式设置,字段声明可以简化为:

employee_ids = fields.Many2many('hr.employee', string='Employees')

好的,试试这个,在 python 边:

class FineAllocation(models.TransientModel):
    _name = 'fine.allocation'
    _description = 'Fine Allocation'
    
    # Use one two many field because you need to information an employee and fine amount
    employee_allocation_ids = fields.One2many('fine.allocation.amount', 'Employees allocations')
    
    def action_allocate_fine(self):
        for rec in self.employee_allocation_ids:
            for rec in self.employee_allocation_ids:
               vals = {
                 'deduction_id': 1,
                 'computation': 'fixed',
                 'fixed': rec.fine_amount,
                 'employee_id': rec.employee_id.id,
               }
               self.env['ke.deductions'].sudo().create(vals)
    

# create the model that will hold the two information and link them to the wizard 
class FineAllocationAmount(models.TransientModel):
    _name = 'fine.allocation.amount'
    _description = 'Fine Allocation'
    
    fine_allocation_id = fields.Many2one('fine.allocation')
    
    employee_id = fields.Many2one('hr.employee', 'Employee', required=True)
    fine_amount = fields.Float('Fine amount')

在您的表单视图中:

<record id="employee_fine_allocation_form" model="ir.ui.view">
        <field name="name">Employee Fine Allocation Form</field>
        <field name="model">fine.allocation</field>
        <field name="arch" type="xml">
            <form string="Employee Fine Allocation">
                <group>
                    <span colspan="4" nolabel="1">This wizard will allocate fines for all selected employee(s) equalling the input fine amount.</span>
                </group>

                <group colspan="4">
                    <separator string="Employees" colspan="4" />
                    <newline />
                    <field name="employee_allocation_ids" nolabel="1" >
                         <!-- Use tree view to fill the information no need to open anather popup -->
                        <tree editable="bottom">
                          <field name="employee_id"/>
                          <field name="fine_amount"/>
                        </tree>
                    </field>
                </group>

                <footer>
                    <button name="action_allocate_fine" string="Allocate Fines" type="object" class="btn-primary" />
                    <button string="Cancel" class="btn-secondary" special="cancel" />
                </footer>
            </form>
        </field>
    </record>

希望对您有所帮助。