Odoo在加载时过滤many2one字段

时间:2022-01-24 20:23:51

In my module I have a many2one field to select workers for a particular task. According to the requirement that field should only display the workers in the current user's department. Simply this is the code,

在我的模块中,我有一个many2one字段来为特定任务选择worker。根据要求,字段应仅显示当前用户部门中的工作人员。这只是代码,

 _columns = {
    'employee_id': fields.many2one('hr.employee', 'Employee'),
}

My problem is how to perform such filteration for a field on load? I tried using functional field in a domain in view xml. but It seems functional field gets its value when saving the particular record.

我的问题是如何在加载字段上执行此类过滤?我尝试在视图xml中使用域中的功能字段。但在保存特定记录时,似乎功能字段会获得其值。

Also I tried adding domain to the field itself, here get_current_user_department is a function returns the department id

另外我尝试将域添加到字段本身,这里get_current_user_department是一个函数返回部门ID

_columns = {
    'employee_id': fields.many2one('hr.employee', 'Employee',domain=[('department_id.id','=',get_current_user_department)]),
}

This generates following error,

这会产生以下错误,

TypeError: is not JSON serializable

TypeError:不是JSON可序列化的

Any suggestion to make this work? Thanks

有什么建议使这项工作?谢谢

2 个解决方案

#1


2  

Also you can take one field for storing current user department you can set default value of current user department.

您还可以在一个字段中存储当前用户部门,您可以设置当前用户部门的默认值。

default_department_id = fields.Many2one('employee.department', 
                                        string='My User',
                                        default='get_department') 

Now you need to create function for set default department.

现在您需要为set default department创建函数。

After that you need to write in XML:

之后你需要用XML编写:

<field name="default_department_id" invisible="1"/>
<field name="employee_id" 
       domain="
           [('department_id','=',default_department_id)]
       "/>

#2


2  

You have to define a new many2one field to save the current user department ID and put the value of the department at loading with default_get() method. Then after, you can put this field on a domain to filter employee that are in the same department than the user.

您必须定义一个新的many2one字段以保存当前用户部门ID,并使用default_get()方法将部门的值置于加载状态。之后,您可以将此字段放在域中,以过滤与用户位于同一部门的员工。

#1


2  

Also you can take one field for storing current user department you can set default value of current user department.

您还可以在一个字段中存储当前用户部门,您可以设置当前用户部门的默认值。

default_department_id = fields.Many2one('employee.department', 
                                        string='My User',
                                        default='get_department') 

Now you need to create function for set default department.

现在您需要为set default department创建函数。

After that you need to write in XML:

之后你需要用XML编写:

<field name="default_department_id" invisible="1"/>
<field name="employee_id" 
       domain="
           [('department_id','=',default_department_id)]
       "/>

#2


2  

You have to define a new many2one field to save the current user department ID and put the value of the department at loading with default_get() method. Then after, you can put this field on a domain to filter employee that are in the same department than the user.

您必须定义一个新的many2one字段以保存当前用户部门ID,并使用default_get()方法将部门的值置于加载状态。之后,您可以将此字段放在域中,以过滤与用户位于同一部门的员工。