콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
1 회신
209 화면

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!

아바타
취소
베스트 답변

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

아바타
취소
관련 게시물 답글 화면 활동
1
3월 21
4632
1
6월 15
5530
2
4월 25
3391
3
11월 24
18923
1
1월 23
2792