Search code examples
pythonxmlodooodoo-11

Get External Data from External API in Tree View Odoo 11


I have a problem. I want to display data from external API in tree view with depends/refresh automatically. Installation is success and I have the data in external url. But they won't appear in the tree view when I requested it. I am still new at this. Please help me. I am stuck at this.

<<<<<<<<<This is my function>>>>>>>>
@api.depends()
def get_folders_request(self):
    # https = urllib3.PoolManager()
    url = ""
    user = ""
    querystring = {"limit":"10","offset":"0","sort":"desc"}
    headers = {"Accept": "application/json"}
    response = requests.get(url, headers=headers, params=querystring, auth=(user, ''))
    res = simplejson.loads(response.text)
    _logger.info("response: %s", res)

    if 'error_code' in res:
        raise UserError(res['message'])

    for record in self:
        record.name = res['name']
        record.email_blacklist = res['totalBlacklisted']
        record.email_subscribers = res['totalSubscribers']
        record.unique_subscribers = res['uniqueSubscribers']
        record.email_count = res['count']
   
<<<<<<<<<<<<<This is my views>>>>>>>>>>>>

<record id="view_sib_template_tree" model="ir.ui.view">
            <field name="name">view.sib.template.tree</field>
            <field name="model">sendinblue.get_folders</field>
            <field name="type">tree</field>
            <field name="priority" eval="8"/>
            <field name="arch" type="xml">
                <tree string="LeadsInBlue">
                    <field name="id"/>
                    <field name="name"/>
                    <field name="email_blacklist"/>
                    <field name="email_subscribers"/>
                    <field name="unique_subscribers"/>
                    <field name="email_count"/>
                </tree>
            </field>
        </record>

Solution

  • As you try doing is not possible. It's important to understand the odoo logical.

    All informations showing in Odoo coming from a DB.

    When you try called a field with a compute method (I think you try a compute method because you set an @api.depends). If you don't have a record the compute will not be called.

    If you want connected a external data to your Odoo, you have 2 possibilities.

    CREATE DATA

    The easier method is create the external data in your database.

    To do this you can create a CRON, this CRON will call your extrenal data and set in DB.

    1. Create a CRON. Called every X Minutes / Hours / Days
    2. In the CRON method, call your external data.
    3. Format data to match with Odoo DB
    4. Search already existing data in you db with self.env['your_model'].search([...])
    5. Create or Update data with method create or write

    ORM METHOD

    In Odoo you have a commun method called read. This method is called each time when the Odoo try read data from cloud. You can overriden this method of your model to return "ephemeral" data.

    This option is for my opinion the better solution BUT this method is very complicated to make it realy 100% sure. And I don't will explain all the solution here.

    Juste for your information.

    1. You need override read method (def read(self, fields,load)).
    2. Remove the basic logical of Odoo to fetch data in DB and replace it by your data.
    3. Overriden method search, create, write, unlink.