Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
4341 Lượt xem
class Example(models.Model):
    _name = "example.model"

team = fields.Many2one("maintenance.team", "Team")

    @api.depends("team")
    def _team_member_ids(self):
        print(self.team.member_ids.ids) # this correctly prints user IDs for users set in that team
        # below domain however returns nothing
        return [('user_id', 'in', self.team.member_ids.ids)]

work_shift_lines_ids = fields.Many2many("hr.employee", relation="shift_one_relation", domain=_team_member_ids)


I would expect this to work but my field work_shift_lines_ids does not offer anything when I select it. 

I expect it to filter only those employees that have user_id in team.member_ids


I checked both models, this is how built in maintenance.team model defines member_ids:


class MaintenanceTeam(models.Model):
     _name = 'maintenance.team'
     
    # some other fields...
    member_ids = fields.Many2many(
        'res.users', 'maintenance_team_users_rel', string="Team Members",
        domain="[('company_ids', 'in', company_id)]")



As you can see member_ids is just Many2many to res.users so when I did self.team.member_ids.ids it will just give me list like [2, 24]  (this works I tested).


For the first part of domain, where I did 'user_id' , my logic is since work_shift_lines_ids is defined as Many2many hr.employee model and hr.employee has user_id field it will take that field and run it trough self.team.member_ids.ids.


I also tried 

[('user_id.id', 'in', self.team.member_ids.ids)]

but again nothing is showing in selection.


I also declared my domain with @api.depends("team") as I would like it to get updated when team changes, not sure if this works since I can't test because my domain is broken but is this logic correct?

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

Hello FarFarAway,

You can try on change method for many2many domain.

team = fields.Many2one("maintenance.team", "Team")

@api.onchange('team')
def onchange_team_id(self):
return {'domain': {
'work_shift_lines_ids': [('user_id', 'in', self.team.member_ids.ids)]
}}

work_shift_lines_ids = fields.Many2many("hr.employee", relation="shift_one_relation")

Hope this may help you!


Thanks & Regards,

Email: odoo@aktivsoftware.com

Skype: kalpeshmaheshwari

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

That works, thank you.

Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 11 22
80
1
thg 6 22
7314
1
thg 7 21
2867
9
thg 2 16
49385
1
thg 10 15
10918