Skip to Content
Menu
This question has been flagged
2 Replies
2409 Views

Hello, 

I have been struggling the past couple of days with a method on an inherited model that tries to sync data with an external API. Everything seems ok to me, but self.write() doesn't sends the updated data to the database.

Can someone tell what am I doing wrong?


Here is a simplified version of my code:


class ResPartner(models.Model):
"""Customer"""

_inherit = "res.partner"

external_id = fields.Char()

    def sync_with_external(self):
        api_key = self.env["ir.config_parameter"].get_param("custom_account_invoicing.external_api_key")

        data = {
            "legal_name": self.name,
            "tax_id": self.vat
        }

        api = ExternalApi(api_key)
        res = api.customers.create(data)

        self.write({"external_id": res["id"]})



Avatar
Discard

Hello,
I think you need to change the field name.

Reason: Odoo already external id field in import and export data.
Thanks.

Author

Already tried another field name, but same results

Author Best Answer

After further analysis, I noticed that Postgres was receiving a ROLLBACK query every time the sync_with_external method was being called. So the problem wasn´t the write() method.

Finally the culprit was a line of code at the end of the custom method:

raise exceptions.Warning(_("Customer synced successfully"))

I thought raising a Warning was innocent enough to be used just to display a success message to the user. Little I knew this was going to cost me several days of rethinking my life choices and almost falling into insanity.

I hope this helps anyone that may fall into this same issue.

Avatar
Discard
Best Answer

Why don't you use

self.external_id = res['id']

Instead of 

self.write({"external_id": res["id"]})

Are you also certain that the method sync_with_external is being executed and that res['id'] is not None. I hope this helps.

Avatar
Discard
Author

1. Using self.external_id = res['id'] does change the instance property value. Even I tested it using odoo shell. But in the database doesn't the field remains empty.

2. The method is manually executed by a button in the form view.

Thanks

But what does print(res['id']) output? If the API doesn't return anything it can't be saved.

Did you do a module upgrade/odoo restart?

Author

Oh sorry, res["id"] returns the correct value every time.
Also I did a module upgrade, odoo restart, even a database wipeout and fresh install, testing it out on docker container instead of the repo build. And all have the same results.

Related Posts Replies Views Activity
1
Dec 24
68
0
Dec 24
41
1
Dec 24
48
0
Dec 24
56
0
Nov 24
64