Skip to Content
Menu
This question has been flagged
1 Reply
9930 Views

I'm trying to get projects where the selected contact is the follower. I need it so I can select for which projects I should generate the report. For this I'm using a wizard with a Many2One field for selecting the contact and a Many2many field I want to use for checkboxes in the view.

Is it possible to set a dynamic domain that will filter the Many2many field?

contact_id = fields.Many2one('res.partner', domain="[('customers_tic_id', '!=', False)]")
project_ids = fields.Many2many(comodel_name='project.project',
                                   relation='project_document_wizard_rel',
                                   column1='document_wizard_id',
                                   column2='project_id',
                                   string='Contacts following projects',
                                   domain="[('project_id', 'in', self.followers_search_to_list())]")




def followers_search_to_list(self):
        tmp_list = list()
        followers = self.env['mail.followers'].sudo().search([('partner_id', '=', contact_id.id), ('res_model', '=', 'project.project')])

        for follower in followers:
            tmp_list.append(follower.res_id)

        return tmp_list

So what I'm doing wrong?

Avatar
Discard
Best Answer

Hi, 

Your question is : can we set a dynamic domain that will filter M2M field based on Another M2O field? 

Answer : Yes you can. 

First, remove the domain from the M2M declaration. Then add a onchange function like this:

@api.onchange('contact_id')
 _onchange_contact_id(self):
    if self.contact_id:
        domain = [('project_id', 'in', self.followers_search_to_list())]
        return {'domain': {'project_ids': domain}}

Upvote if this helps. If not, you can write back for further analysis.
Regards.
Avatar
Discard
Author

Thanks! I only needed to change 'project_id' to 'id'. And you forgot to say that I needed to add compute='_on_change_contact_id' to M2M field. Is there a way to not show any M2M records until the contact is selected? Now I get all existing projects until I select the contact.

Author

I can't click on any of the checkboxes!?

Hi,

1- I did not forget to mention that the field should have compute attribute because simply it should not.

2- Not show M2M record until contact is selected : in onchange_contact_id, i only added the condition where contact is set. In this case, you can add 'else' then apply another domain that suits your need.

3- you can't click on any of the checkboxes because you made the field compute... compute field is readonly. Since that is not what i suggested, remove compute attribute.

but to enlighten you more, even if a field is computed, you can still add records manually by adding the attribute "inverse".

Hope this helps you.

Author

yeah I figured that about compute field out, so I removed it and it saves. But the problem that I've got now is when the record is saved and when I open it again (go to a different view and back) there are again displayed all projects. But at least the ones that were selected remain selected.

@Tabla : I also have the same problem while using onchange for domain.You got any other solutions ?

Author

You need to specify the problem a bit more. But mostly the solution suggested worked for me. If you created a post with the problem explained and some code then post me the link in the comment.

Hi.

Can I do this with Automated Actions?

I have a many2one field (res.partner) and a many2many field (account.move)

When I select the partner, I want only to show the account move documents related with the partner selected.

I'm wondering if it's possible to achieve this with a automated actions and a python code.

Thanks

Thank you, it works !!

Related Posts Replies Views Activity
0
Nov 22
80
1
Jun 22
5673
1
Jul 21
1766
1
Jul 21
2949
2
May 17
10098