如何在 One2many 字段或关系 table 上基于分组进行搜索视图

How to do search view based on group by on a One2many field or on a relation table

我在 Openerp 7 中遇到了一个新的搜索问题。当我尝试按关系 table 的某个字段进行分组时,它不起作用。 例如,我需要来自合同模块 (hr_contract Table) 的职位名称(数据库字段:job_id),条件为:primary_contract(自定义字段)将是 'Yes',但我将根据员工模块 (hr_employee Table) 的合同职位 Table 进行分组。然而,工作(数据库字段:job_id)已经存在于员工模块(hr_employee Table)中,但我不想在员工 table 的工作字段上分组.我需要根据合同 table 的职务进行分组。请给我任何解决方案。 这是我的代码,

在employee_view_xml,

<record id="extend_view_employee_filter" model="ir.ui.view">
    <field name="name">Employees</field>
    <field name="model">hr.employee</field>
    <field name="inherit_id" ref="hr.view_employee_filter"/>
    <field name="arch" type="xml">
        <xpath expr="//field[@name='name']" position="after">
            <filter icon="terp-go-month" 
                string="New Employee"
                domain="[('joining_date','&lt;=',(context_today()+relativedelta(day=31)).strftime('%%Y-%%m-%%d')),('joining_date','&gt;=',(context_today()-relativedelta(day=1)).strftime('%%Y-%%m-%%d'))]"
                help="Employees who have joined current month ...." />

            <filter icon="" 
                string="Unsigned Contract"
                domain="[('contract_ids', '=', False)]"
                help="Employees who have not any contract on Contract module ...." />

            <!-- <filter icon="" 
                string="Extend Contract"
                domain="['&amp;',('contract_ids.primary_contract', '=', 1),('contract_ids.date_end', '&gt;', (context_today().strftime('%%Y-%%m-%%d')))]"
                help="Employees who need to extend contract ...." /> -->

            <filter icon="terp-go-month" 
                string="Resigned Employee"
                domain="[('leaving_date','&lt;=',(context_today()+relativedelta(day=31)).strftime('%%Y-%%m-%%d')),('leaving_date','&gt;=',(context_today()-relativedelta(day=1)).strftime('%%Y-%%m-%%d'))]"
                help="Employees who have resigned current month ...." />
        </xpath>           
        <xpath expr="//group[@string='Group By...']/filter[@string='Company']" position="after">
            <filter string="Joining Date" domain="[]" context="{'group_by': 'joining_date'}"/>
            <filter string="Team" domain="[]" context="{'group_by': 'team_id'}"/>
            <filter name="get_group_by_job_of_contracts" string="Job (Contract)" domain="[('contract_ids.primary_contract', '=', 1)]" context="{'group_by': 'contract_ids[0]'}" />
        </xpath>
    </field>
</record>

在employee.py,

'contract_ids': fields.one2many('hr.contract', 'employee_id', string='All Contracts', required=False, domain=[('active','=',True)], store=True)

在contract.py,

'employee_id': fields.many2one('hr.employee', "Employee", required=True)
'job_id': fields.many2one('hr.job', 'Job Title')

提前致谢

无法按 many2manyone2many 字段分组。列表视图不是为它设计的。您已经了解这一点,因为您正在尝试对员工使用第一份合同。但这不管用。

如果你真的想用第一个合约进行group by,在hr.employee上定义一个与contract_ids.job_id相关的相关字段:

_columns = {
    'first_contract_job_id': fields.related(
        'contract_ids', 'job_id', type='many2one',
        relation='hr.job', string='First Contract's Job', store=True),
}

那应该拿第一个合约的job_id。存储这个字段很重要,因为 Odoo 不能按非数据库持久字段分组。 但我不完全确定它会起作用,因为 one2many 上此类相关字段的第一个示例在 Odoo V8 中使用,例如 product.template。试试吧。

如果您与一名员工有多个合同,您应该使用计算字段而不是相关字段。