Skip to Content
Меню
Вам необхідно зареєструватися, щоб взаємодіяти зі спільнотою.
Це запитання позначене
2 Відповіді
991 Переглядів

Hi!

I have a category​ model, and a subcategory​ model. Subcategories have a single parent category (x_parent_category_id), and categories can have many child subcategories.

  • Companies
    • Small
    • Medium-sized
    • Large
  • Associations
    • Regular non-profit
    • NGOs
  • Fund
    • Investment fund
    • Pension

I have two many-to-one relationships on my customers to know their category (x_category_id) and their subcategory (x_subcategory_id) .

I'd like to be able to set a domain on the x_subcategory_idrelationship so that subcategories are always a child of the parent category.

So, I have trouble using a domain like [('x_parent_category_id', '=', x_category_id)]​.

It seems both Odoo Studio and the debug mode forms hate x_category_id​ as a floating value here, raising a NameError​. I also naively tried self.x_category_id​ unsuccessfully.


Is there any way to achieve this domain setup?


Right now, I'm checking that the field is properly set using an automation. It's a bit cumbersome and surely there's a fancier way to achieve this!


I use Odoo Online, so version 18.

Аватар
Відмінити
Найкраща відповідь

Hi,
To set a domain filter on x_subcategory_id based on the selected x_category_id in Odoo Online v18, you need to use a special syntax since direct field references don't work in static domains. Here's how you can achieve this:
You need to use the parent prefix to reference the current record's field value in your domain. Try this domain on your x_subcategory_id field:

[('x_parent_category_id', '=', parent.x_category_id)]
This tells Odoo to filter subcategories where their parent category matches the currently selected category on the form.
  1. The parent keyword in domains refers to the current record being edited
  2. parent.x_category_id dynamically accesses the value of the x_category_id field on the current record
  3. This creates a dynamic filter that changes based on what category is selected

Regards,

Jishna

Accurates

Аватар
Відмінити
Автор Найкраща відповідь

@Accurate I tried to comment on the reply unsuccessfully.

Trying to plug this into Odoo via the debug form yields the following error:

NameError: name 'parent' is not defined
Аватар
Відмінити
Related Posts Відповіді Переглядів Дія
2
бер. 15
9308
1
жовт. 22
3831
8
лют. 17
8373
0
січ. 17
5880
2
бер. 15
9277