Skip to Content
Menu
This question has been flagged
4 Replies
9532 Views

How can we use dynamic domains in new Odoo API?

I'm trying to do it, I have an Many2one field, and a compute One2many field. Options to be select by Many2one field should be in this One2many field.

Here are my code lines:

    days = fields.One2many('af.week.day', string='Class days', compute='_compute_days',
                           help='Week days in which this group has classes')
    day_of_week = fields.Many2one('af.week.day', string='Day of week', domain="[('id','in',days)]")

Avatar
Discard
Author

Any idea? In old api we can use something like this: def onchange_type(self,cr,uid,ids, type,context=None): product_obj = self.pool.get('product.product') product_ids = product_obj.search(cr,uid, [('type','=',type)]) return {'domain':{'product_id':[('id','in',product_ids)]}} But in new api... Thanks in advanced!

Author

No anwers?

Send the real example what you did from your side. here in comment you have given the example of onchange method what you want to do it. you can not set dynamic domain on onchange method. give more description of your requirement. dynamic domain can be set by overriding search method of that particular model.

Best Answer

In the new Odoo API it is the same as in Version 7.0. Look at this example:

@api.onchange('field')
def onchange_field(self):
    if condition_a:
        return {
            'domain': {
                'field_b': [('domain', '=', 'something')],
            },
        }
    else:
        return {
            'domain': {
                 'field_b': [('domain', '=', 'something_else')],
            },
        }

Avatar
Discard
Best Answer

You can follow this: https://youtu.be/XGqXEL2qQmE 

Hope it helps, Thanks

Avatar
Discard
Best Answer

Hi,

Other than returning domain in onchange method, you can use the web domain field module from oca for the same:  Web Domain Field


See this: Return Dynamic Domain For Field In Odoo


How to use:

.. code-block:: xml

<field name="product_id_domain" invisible="1"/>
<field name="product_id" domain="product_id_domain"/>


.. code-block:: python

product_id_domain = fields.Char(
compute="_compute_product_id_domain",
readonly=True,
store=False,
)

@api.multi
@api.depends('name')
def _compute_product_id_domain(self):
for rec in self:
rec.product_id_domain = json.dumps(
[('type', '=', 'product'), ('name', 'like', rec.name)]
)

Returning domain from the onchange function: How To Give Domain For A Field Based On Another Field

Thanks

Avatar
Discard
Best Answer

Dynamic domain can only be implemented in view.  You can add domain attribute for the field tag and it can accept any other fields' name within the domain.

Avatar
Discard