Skip to Content
Menu
Dette spørgsmål er blevet anmeldt
1 Svar
150 Visninger

Hi everyone,

I want to create chained selection fields in Odoo, where the value of one field depends on the value of another.

For example:

  • When I select a “Category”, it should call an API and load the options for “Subcategory”.
  • When I select a “Subcategory”, it should call another API and load data for the “Item” field.

All the data for these fields comes from external API calls

Also, when a user selects a value, I want to save the entire record (from the API response) for that option, not just the name or ID, so I can use it later for reporting or audits.


I’m looking for the correct Odoo approach to implement this.

Any examples or best practices would be appreciated!

Avatar
Kassér
Bedste svar

Hi,


To implement chained selection fields in Odoo where one field depends on another, you can use @api.onchange to trigger API calls when a field changes. For example, selecting a Category can fetch Subcategories from an external API, and selecting a Subcategory can fetch Items.


It’s recommended to store the entire API response in a JSON or Text field, so you can reuse it later for reporting or audits. Dynamic options for dependent fields can be populated at runtime using the API response.


For best practices, avoid using API calls in computed fields, consider caching if API performance is an issue, and use Many2one fields with dynamic domains for larger datasets. Alternatively, syncing API data periodically into Odoo models can improve performance and maintain relational integrity.


Sample Code:-


category = fields.Selection([], string="Category")

    subcategory = fields.Selection([], string="Subcategory")

    item = fields.Selection([], string="Item")


    # Optional JSON fields to store API responses

    category_data = fields.Text(string="Category Data")

    subcategory_data = fields.Text(string="Subcategory Data")

    item_data = fields.Text(string="Item Data")


    @api.model

    def get_category_selection(self):

        # Fetch from API

        response = requests.get("https://api.example.com/categories")

        if response.status_code == 200:

            data = response.json()

            # Save complete data for audit

            self.category_data = json.dumps(data)

            # Return selection list as tuples

            return [(d['id'], d['name']) for d in data]

        return []


    @api.onchange('category')

    def _onchange_category(self):

        if self.category:

            response = requests.get(f"https://api.example.com/categories/{self.category}/subcategories")

            if response.status_code == 200:

                data = response.json()

                self.subcategory_data = json.dumps(data)

                return {

                    'domain': {},

                    'value': {'subcategory': False},

                    'options': {'subcategory': [(d['id'], d['name']) for d in data]},

                }


    @api.onchange('subcategory')

    def _onchange_subcategory(self):

        if self.subcategory:

            response = requests.get(f"https://api.example.com/subcategories/{self.subcategory}/items")

            if response.status_code == 200:

                data = response.json()

                self.item_data = json.dumps(data)

                return {

                    'domain': {},

                    'value': {'item': False},

                    'options': {'item': [(d['id'], d['name']) for d in data]},

                }



Hope it helps

Avatar
Kassér
Related Posts Besvarelser Visninger Aktivitet
1
mar. 21
4617
1
jun. 15
5504
2
apr. 25
3364
3
nov. 24
18893
1
jan. 23
2761