Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
2 Risposte
990 Visualizzazioni

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.

Avatar
Abbandona
Risposta migliore

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

Avatar
Abbandona
Autore Risposta migliore

@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
Avatar
Abbandona
Post correlati Risposte Visualizzazioni Attività
2
mar 15
9308
1
ott 22
3831
8
feb 17
8373
0
gen 17
5880
2
mar 15
9277