I've made a module to run code when Save (write) action is made when editing products (product.template). The purpose is to request an external URL to update this product in my website (a script that updates website using API).
The odoo function is:
class ProductTemplateExtra(models.Model):
_inherit = "product.template"
def write(self,vals):
self.ensure_one()
resultat = super().write(vals)
urlupdate = 'https://...sript.php?i=%s' % (self.id)
try:
f = urlopen(urlupdate)
except:
print("An exception occurred")
return resultat
The stript is running fine, but the urlopen is executed BEFORE super().write(vars), and in the script.php I'm getting the OLD values, not the updated values from product.template (just after this, the record is saved and the api gets the updated values, but not when the action is executed).
Using Odoo 15CE
What I'm doing wrong?
Yes, you will get old values in your script because values are saved in the database when code executes a return statement.
In your case, I suggested passing one more parameter and getting values from the 'vals' variable. And add conditions to avoid multiple URL hits.
For example: Following code executes only if the product name is changed.
resultat = super().write(vals)
if 'name' in vals:
urlupdate = 'https://...sript.php?i=%s&product_vals=%s' % (self.id, vals)