0

我有一个想要满足的要求是从数据库中获取一个值并在 UI 中显示,我们有一个应用程序,我们可以将“项目”分配给很多人,所以我必须在人员信息中显示当前项目的字段分配给他/她。

我正在考虑创建一个函数来使用“search()”方法检索该数据以获取所需的字段。

#for the moment i have added the variable in the class
project_name = fields.Char('Project Name', compute='_compute_get_employee_project')

#this is the function i am building (trying)
@api.model
    def _compute_get_employee_project(self):
        #verify the employee belongs to a project
        employee_task = self.env['project.task'].search([('user_id','=',self.user_id.id),('status','=','assigned')],limit=1)
        employee_project = self.env['project.project'].search([('id', '=', employee_task.project_id.id)], limit=1)

我的最终目标是能够获取员工当前分配的项目。

感谢您的时间!!

4

2 回答 2

2

计算方法必须将计算值分配给该字段

您可以使用取决于相关用户的 many2many 字段来计算分配给所选用户的项目。如果只有一个项目分配给所选用户,Odoo 将显示一个项目。

示例

class ResEmployee(models.Model):
    _inherit = 'hr.employee'

    project_ids = fields.Many2many('project.project', compute='_get_assigned_projects', string='Projects')

    @api.depends('user_id')
    def _get_assigned_projects(self):
        project_task = self.env['project.task']
        for employee in self:
            if employee.user_id:
                employee.project_ids = [(4, project_id, 0) for project_id in
                                        project_task.search(
                                            [('status', '=', 'assigned'),
                                             ('user_id', '=', employee.user_id.id)]
                                        ).mapped('project_id.id')]
            else:
                employee.project_ids = False

使用以下代码在工作移动字段之前显示分配的项目标签:

<record id="view_employee_form" model="ir.ui.view">
    <field name="name">hr.employee.form</field>
    <field name="model">hr.employee</field>
    <field name="inherit_id" ref="hr.view_employee_form"/>
    <field name="arch" type="xml">
        <field name="mobile_phone" position="before">
            <field name="project_ids" widget="many2many_tags"/>
        </field>
    </field>
</record>  
于 2021-02-28T09:06:29.020 回答
1

您可以执行以下操作:

#for the moment i have added the variable in the class
project_name = fields.Char('Project Name', compute='_compute_get_employee_project')

#this is the function i am building (trying)
@api.multi
@api.depends('user_id')
def _compute_get_employee_project(self):
    for emp in self:
        #verify the employee belongs to a project
        employee_task = self.env['project.task'].search([('user_id','=',self.user_id.id),('status','=','assigned')],limit=1)
        employee_project = self.env['project.project'].search([('id', '=', employee_task.project_id.id)], limit=1)
        emp.project_name = employee_project.display_name

或者你可以走另一条路

project_ids = fields.One2many('project.project', compute='_compute_get_employee_project')

@api.multi
@api.depends('user_id')
def _compute_get_employee_project(self):
    for emp in self:
        emp_task_ids = self.env['project.task'].search([('user_id','=', emp.user_id.id),('status','=','assigned')])
        emp_project_ids = emp_task_ids.mapped('project_id')
        emp.write({
            'project_ids': [(6, 0, emp_project_ids.ids)]
        })

Char请注意以前的字段定义和新的字段定义之间的区别One2manylimit=1添加的第一种情况与没有限制的第二种情况之间的区别。这是因为用户可能被分配到不同项目中的多个任务。

如果一切正常,请随时告诉我们。

于 2021-02-28T07:57:34.970 回答