Odoo how to filter many2one field values in one2many field

1.6k views Asked by At

I have many2one field name_id. Usually when I use it in my_model.xml code below in this field I can select from all the values written in my.model.line. In this case I want to filter values and see the list of only these values which are written in current my.model.

my_model.xml

    <record model="ir.ui.view" id="view_my_model_form">
        <field name="name">my.model.form</field>
        <field name="model">my.model</field>
        <field name="arch" type="xml">
            <form string="My Model">
            <header>

                        <field name="my_model_line">
                            <tree>
                                <field name="name"/>
                            </tree>
                         </field>
               <notebook>
                    <page>
                        <field name="supply_conditions_status">
            <tree string="My model Lines">
                                <field name="name_id"/>
                            </tree>

Classes:

class SupplyConditions(models.Model):
    _name = 'supply.conditions'

    name_id = fields.Many2one('my.model.line', string='Product')
    model_id = fields.Many2one('my.model')


class MyModelLine(models.Model):
    _name = 'my.model.line'

    name = fields.Char('Name')
    my_model_id = fields.Many2one('my.model')

class MyModel(models.Model):
    _name = 'my.model'

    title = fields.Char('Title')
    my_model_line = fields.One2many('my.model.line', 'my_model_id')
    supply_conditions_status = fields.One2many('supply.conditions', 'model_id')

For clearer understanding - example:

I have form view of my.model (field name = 'first') and my.model (field name = 'second').

In my.model (name = 'first') In my_model_line name field added data: 'item1', 'item2'

In my.model (name = 'second') In my_model_line name field added data: '1item', '2item'

Now:

In my.model (name = 'first') supply_conditions_status name_id field I see selection of: '1item', '2item, item1', 'item2'

Need:

In my.model (name = 'first') supply_conditions_status name_id possibility to select only from 'item1', 'item2'

How can I achieve this?

1

There are 1 answers

0
Amal On

You can achieve this by simply giving domain to your many2one field. try this:

name_id = fields.Many2one('my.model.line', string='Product', domain=lambda self: [('my_model_id', '=', self.model_id)])