Odoo how to filter many2one field values in one2many field

馋奶兔 提交于 2019-12-11 05:59:25

问题


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:


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)])



来源:https://stackoverflow.com/questions/46294858/odoo-how-to-filter-many2one-field-values-in-one2many-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!