Skip to Content
Menu
This question has been flagged

I have a model named "student" that had a many2many relation with a model named "authorized" and another model named "authorized" with a many2many relation with the model "student". In the other hand, I have a third model named "exit_control" that contains a many2one relation with the model "student".

I wanna a second field many2one in the model "exit_control" that shows only the authorizeds related with the student selected.

In other words: if student1 is related with authorized6 and authorized8, and the complete table of authorized had 20 authorizeds, and I select the student1 in the field student_id of the model exit_control, I wanna show a field many2one with the authorized6 and authorized8.

Student model had:

authorized_ids = fields.Many2many('aula10.authorized', string='Authorizeds')

Authorized model had:

authorized_for_ids = fields.Many2many('aula10.student', string='Can get this student:')

Exit_control model had:

student_id = fields.Many2one('aula10.student',string='Student')
Given_to_id = fields.Many2one('aula10.authorized', string='Given to:')

Given_to_id is the field that I wanna use for show the filtered authorizeds.

I tried to use the attribute domain in view and in model, but was impossible for me.

Can you help me, please?

Avatar
Discard
Best Answer

Hi Daniel,

to use domain, you need a searchable field to filter. I guess, you should add a related field to the model Exit_control:

authorized_ids = fields.Many2many(
    related="student_id.authorized_ids",
    store=True,
)


Than, use the domain in the xml:

<field name="authorized_ids" invisible="1">
<field name="Given_to_id"
       domain="[('id', 'in', authorized_ids and authorized_ids[0] and authorized_ids[0][2] or False)]"
/>

Take into account that depending on Odoo version, there are sometimes troubles in using domain by m2m fields. Thus, I used so complex one. However, ('id', 'in', authorized_ids) might work in certain cases as well.

Avatar
Discard
Related Posts Replies Views Activity
1
Apr 22
1318
1
Jun 21
2801
2
Mar 24
405
1
Feb 24
931
1
Jul 22
4054