Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
4 Trả lời
15380 Lượt xem

can any body please tell me how to filter many2one field data

model name is "project.task"

i have fields called

company_id, department_id and "assigned_to"

all are many2one fileds

based on company id and department id i want to filter the "assigned_to" field

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

There is no direct relationship between User (Assigned To) and Department.

You will have to add a department_id field in the user so that you can use if to filter the records.

Ex:

<field name="user_id" domain="[('company_id', '=', company_id), ('department_id', '=', department_id)]"


Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

You can use the "domain" attribute. For example:

<field name="user_id" domain="[('login', 'ilike', 'test.com')]"/>

In the example above, only users with an email "test.com" will be available for selection in the many2one drop down.

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

You can return domain from onchange method, which solve your issue. 

@api.onchange('company_id', 'department_id') 
def onchange_m2o(self):
if self.company_id and self.department_id:
users = self.department_id.member_ids.mapped('user_id')
        if users:
    return {'domain' : { 'user_id' [('id','in', users.ids)] }}


Make sure department must have employees.

This method add domain to the Assigned to field at runtime based one the value you selected on the task.

Ảnh đại diện
Huỷ bỏ
Tác giả

thanks for your answer.

i tried this one i got type error

TypeError: string indices must be integers

See the updated answer.

Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 3 25
6082
0
thg 12 24
1190
2
thg 1 24
1868
3
thg 10 23
8965
1
thg 9 23
3402