how-to-fetch-multiple-addresses-from-a-partner-via-api-in-odoo-18.j

How to Fetch Multiple Addresses From a Partner via API in Odoo 18?

Fetch Multiple Addresses From a Partner via API: Odoo ERP system is a robust and highly configurable ERP solution. It enables businesses to oversee all aspects of their operations, such as sales, inventory, accounting and partner management from one unified platform. One powerful feature in Odoo is managing partners by storing multiple addresses per partner.

Odoo API, you can retrieve these addresses programmatically, making it easier to integrate Odoo with external systems like CRM tools, e-commerce platforms, or other third-party services. This API-based approach allows you to access paginated data, ensuring that the system handles large datasets efficiently, especially when working with a large number of partners and their corresponding addresses.

We will explore how to fetch multiple addresses from the res.partner model using an API call in Odoo. This approach allows retrieving paginated partner addresses along with their child addresses in JSON format, making it easier to integrate with external systems.

How the get_list Method Retrieves Data from APIs and Databases

The get_list method retrieves partner addresses from the res.partner model with support for pagination and child addresses. It:

  • Filters partners based on a given domain.
  • Implements pagination to handle large datasets efficiently.
  • Fetches child addresses linked to each partner.
  • Returns results in JSON format for easy API integration.

This method ensures structured and optimized retrieval of partner addresses in Odoo

Implementing the get_list Method

The get_list method retrieves partner addresses from the res.partner model with pagination and child address support. It filters partners based on a given domain and returns a structured JSON response.

1. Pagination and Filtering in Odoo Search Method

				
					import math
def get_list(self, domain=[], page_num=1, page_size=10):
    filter = domain
    total_rec = len(self.search(filter))
    pages_count = math.ceil(total_rec / page_size)
    offset = (page_num - 1) * page_size
    partner_address = self.search(filter)[offset:offset + page_size]
    response = {}
    data = []

				
			

2. Formatting Partner Address Data for Response

				
					 for o in partner_address:
        obj = {}
        obj['id'] = o.id
        obj['type'] = o.type
        obj['name'] = o.name
        obj['street'] = o.street
        obj['street2'] = o.street2
        obj['city'] = o.city
        obj['state_id'] = [o.state_id.id if o.state_id else '', o.state_id.name if o.state_id else '']
        obj['country_id'] = [o.country_id.id if o.country_id else '', o.country_id.name if o.country_id else '']
        obj['email'] = o.email
        obj['phone'] = o.phone
        obj['zip'] = o.zip
        obj['is_default'] = o.is_default
        obj['address_type'] = o.address_type

				
			

3. Processing Child Addresses and Structuring the Final Response

				
					 address_data = []
        for child in o.child_ids:
            address_obj = {
                'id': child.id,
                'name': child.name,
                'type': child.type,
                'street': child.street,
                'street2': child.street2,
                'city': child.city,
                'state_id': [child.state_id.id if child.state_id else '',
                             child.state_id.name if child.state_id else ''],
                'country_id': [child.country_id.id if child.country_id else '',
                               child.country_id.name if child.country_id else ''],
                'email': child.email,
                'phone': child.phone,
                'is_default': child.is_default,
                'address_type': child.address_type
            }
            address_data.append(address_obj)

        obj['child_addresses'] = address_data
        data.append(obj)

    response['data'] = data
    response['pages_count'] = pages_count
    return response

				
			

Implementation: Leveraging API Call to Retrieve Multiple Partner Addresses

Odoo makes the ability to retrieve multiple addresses for a partner with one API call an invaluable feature when integrating it with external systems. We provide a custom endpoint that returns these partner addresses in JSON format in paginated fashion, enabling you to efficiently retrieve large sets of data without overloading your server- or client-side application.

API Endpoint:

GET:{{localhost}}/api/res.partner/action/get_list?token={{token}}&args=[[(‘id’, ‘=’, 7)]]

Breaking Down the API Call:

  • {{localhost}}: Replace with your Odoo server URL.
  • /api/res.partner/action/get_list: The API method that fetches partner addresses.
  • token={{token}}: Authentication token required for API access.
  • args=[[(‘id’, ‘=’, 7)]]: Search criteria (domain filter) to fetch partner with ID 7.

Understanding an API Response and Data Returned

Response formats for API implementations are of critical importance in how data is presented to clients or external systems that use them. When retrieving partner addresses from Odoo via custom API endpoints, API responses must be structured so as to be easily understood, used, and integrated into external systems.

				
					{
   "success": {
       "data": [
           {
               "id": 7,
               "type": "invoice",
               "name": "mobile",
               "street": "MG roadyus",
               "street2": "addr",
               "city": "dist ",
               "state_id": [594, "Kerala”],
               "country_id": [104,"India"],
  "email": "testuser@gmail.com",
               "phone": "6559985199",
               "zip": "533483",
               "is_default": false,
               "address_type": "home",
               "child_addresses": [
                   {
                       "id": 126,
                       "name": "mobile",
                       "type": "invoice",
                       "street": "bdhshsjsj",
                       "street2": "xbhxjxk",
                       "city": "hshshs",
                       "state_id": [ 578,"Andhra Pradesh" ],                                       
                       "country_id": [104,"India"],
                       "email": "testuser@gmail.com",
                       "phone": " +91 75589 85199",
                       "is_default": false,
                       "address_type": "other"
                   }]}]
       "pages_count": 1
   }}

				
			

Efficiently Integrating Odoo Partner Addresses via API

At Odoo, we’ve demonstrated how to quickly access multiple addresses from its res.partner model with an API call. By creating a custom API endpoint and retrieving paginated partner addresses in JSON format, this data can easily integrate with external systems.

Pagination ensures the system can easily handle large datasets without overburdening client or server systems, while JSON formatting makes the data easily digestible by external applications.

The get_list method in Odoo provides a robust way to:

  • Fetch multiple addresses associated with a partner.
  • Implement pagination for better performance.
  • Structure data in a JSON-friendly format.
Picture of Madhu Satya Deva Raju

Madhu Satya Deva Raju

I'm Madhu Satya Deva Raju P, a Junior Odoo Developer passionate about building solutions through Odoo development, customization, and integration. I share insights and experiences here to help others grow in the ERP ecosystem.

Get a free quote