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

标签 python odoo odoo-12 odoo-13

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

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

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

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

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

我的代码是这样的

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')

这是用于向导记录 View

<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 字段显示默认 View 。

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 列表中显示该字段,您可以扩展默认 View 或定义您的 View ,请尝试以下操作之一:

  1. 扩展默认 TreeView 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. 定义一个新的 TreeView 并强制 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 字段显示特定 View ,请在上下文中传递 tree_view_ref:

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

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

编辑:

当就地定义 TreeView 时,将 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_id 如果您将其与 employee_ids 进行比较,则会丢失在 hr_payroll 向导模型中。

在您的问题中,您可以看到 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 - 向 odoo many2many 字段添加额外的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62698202/

相关文章:

jquery - 我们如何在odoo 12网站上制作可编辑的表单? (使用js/jquery编辑/保存表单)

python - 在 django 请求之间共享单个连接

nginx - 异常 : bus. 总线不可用 - Odoo 10

python - Odoo扩展与继承

javascript - ODOO报告自动将字符<转换为<

python - 尝试使用 mail_thread 时 Odoo 继承错误

python - 如何修复 ImportError : No module named 'passlib

python - 使用 read() 方法从 Amazon S3 读取大型 JSON 文件时出现内存错误

python - 根据实例化的是异步实例还是同步实例返回包装器

python - 在装饰器包装器函数中访问装饰器参数