Dynamic domain is applied when changing another field value - odoo

class procurement(models.Model)
    _name="procurement"

    procurement_line_ids = fields.One2many(comodel_name='procurement.line', inverse_name='procurement_id', string='Procurement Lines')
    global_procurement = fields.Boolean("Global Procurement",default=True)

class procurement_line(models.Model)
    _name="procurement.line"

    procurement_id = fields.Many2one(comodel_name='procurement', string='Procurement')
    warehouse_id = fields.Many2one(comodel_name='stock.warehouse', string='Warehouse')

class stock_warehouse(models.Model)
    _name="stock.warehouse"

    is_default_warehouse = fields.Boolean(string="Is Default Warehouse?",default=False)

enter image description here

If global_procurement is True, I want to load only the standard stores in the supply line, otherwise I want to load all the warehouses. So how could I do this.

+4
source share
2 answers

We can try as follows.

  • Value in context. For instance:

    <field name="warehouse_id" 
           context="{'global_procurement': parent.global_procurement}"/>
    
  • Check the value of context_search () of the stock.warehouse object. For instance:

    @api.model
    def name_search(self, name, args=None, operator='ilike', limit=100):
        if self._context and self._context.get('global_procurement'):
            default_list = [1,2,3] # set your logic to search list of default warehouse
            return self.browse(default_list).name_get()
        return super(Warehouse, self).name_search(name=name, args=new_args, operator=operator, limit=limit)
    

I wrote a response on the air. I have not tried.

+3
source

I did this by simply defining the domain in the field (idea is taken from @Odedra answer).

<field name="warehouse_id" required="1" domain="[('field_name','=',parent.global_procurement)]" options="{'no_create': True, 'no_quick_create':True, 'no_create_edit':True}" />
0
source

Source: https://habr.com/ru/post/1681511/


All Articles