Skip to Content
Menu
This question has been flagged

I would like to add two fields to a view, category and subcategory, both meant to be fields where the user can choose from a ser of options.

But I would like the options on subcategory to depend on the value that is currently selected on category.


An illustrative example:

    Category could be one of [Arts, Science]

    Subcategory,

        if they picked Arts, one of [Literature, Music, Painting]

        if they picked Science, one of [Biology, CS, Math]


Can you help me accomplish this?


Also, if you know of a module in Odoo which already does a similar thing I could just look at the code and learn from that.


Edit: Thanks niyas, I'll try it out. I'll accept your answer as soon as I get enough karma.

Avatar
Discard
Best Answer

Hi,

Seems you need to apply domain to a field based on another field, if you are using odoo 13 and below, you can follow the approach of returning domain from the onchange function.

See:- How To Give Domain For A Field Based On Another Field


If you are using odoo 14, the above method get deprecated. So you can follow the below procedure using a third party module from OCA.

Module: Web Domain Field


How to use?

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

The field used as domain must provide the domain as a JSON encoded string.

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


Thanks

Avatar
Discard
Related Posts Replies Views Activity
3
Jul 20
10015
4
Oct 24
3682
0
Nov 16
2750
1
Aug 15
3603
0
Mar 15
3921