Skip to Content
Menu
This question has been flagged
2 Replies
1975 Views

Hello. is there any way of making Many2many field related to the Many2one field?

Avatar
Discard
Best Answer

Hi,

Consider the following example,in which i am added a Many2many field in Purchase module which shows Sale Orders of particular partner selected from a Many2one field

class PurchaseOrder(models.Model):
_inherit = 'purchase.order'

sale_order_ids = fields.Many2many('sale.order')

Then, we need to set up domain for that Many2many field for showing Sale Orders of selected partner.

@api.onchange('partner_id')
def _on_change_partner_id(self):
    print(self.partner_id)
    return {'domain': {'sale_order_ids': [('partner_id', '=', self.partner_id.id)]}}

Hope it will work for you.

Avatar
Discard
Best Answer

Hello

you can add m2o filed like

    name_field_id= fields.Many2one(string='Field Name', comodel_name='model name', ondelete='restrict')

then add related field like:

    field_ids = fields.Many2many(string="Name",related=" name_field_id.field_name")  

    field_name is name in your model(comodel_name), and must be m2m.

    

Avatar
Discard