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

时间:2020-07-02 14:06:10

标签: python odoo odoo-12 odoo-13

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

  1. 单击“精细分配”菜单以打开向导。该向导具有与hr.employee相关的many2many关系字段,称为employee_ids。这将使您能够选择批雇员并一起分配罚款。

2。有一个名为fine_amount的字段,在每行中为员工分配个人罚款,然后单击按钮分配罚款。

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

如果我用此字段扩展hr.employee表单,则它不在many2许多行中选定的字段之内。

我的代码如下

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>

2 个答案:

答案 0 :(得分:1)

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

(模型,类型)请求视图时,将返回优先级最低的与模型和类型匹配的视图(这是默认视图)。

要在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

Edit2:

具有以下定义:

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_id向导模型中的employee_ids进行比较,则会缺少

hr_payroll

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

deductions_idEmployees

如果通过传递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 ... 作为string属性,则可以避免该错误,第二列(column2属性)应自动生成,因为它是可选的。您可以尝试以下定义:

Employees

属性employee_ids = fields.Many2many('hr.employee', 'hr_employee_fine_allocation_rel', 'payslip_id', string='Employees') relationcolumn1是可选的。如果未指定名称,则会根据模型名称自动生成名称。

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

column2

答案 1 :(得分:1)

好,尝试一下,在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>

希望对您有帮助。

相关问题