Skip to Content
Menu
This question has been flagged
3 Replies
5092 Views

Hi everyone...I'm totally new in Odoo. I have some problems and I spent over a month to solve it but I wasn't succeed. Here is my problem: Currently I work on integration one java project with odoo, so I need to manage that java project with the MySQL database in background. So I need to send some fields those I added to res.partner object by my self to the MySql data base after clicking on "Save" button, I need it for both cases: create new or update existing partner and I also need to get id of current partner how I could send it also to MySQL 

Thank you in advance

Best 

Misa

Avatar
Discard

Can you make your question with your previous attempts?

Best Answer

Hi,

You can use SQLAlchemy with odoo, an open source SQL toolkit and object-relational mapper (ORM) for Python.

# -*- coding: utf-8 -*- 
from odoo import fields, models, api
from sqlalchemy import *

class ResPartner(models.Model):
    _inherit = 'res.partner'
     
     def mysql_connect():
     
        mysql_cnx_info = { 'host': 'mysql_hots', 'login': 'login', 'password': 'password', 'db_name': 'db_name', }
        param = "{login}:{password}@{host}/{db_name}".format(login=mysql_cnx_info['login'], password=mysql_cnx_info['password'], host=mysql_cnx_info['host'], db_name=mysql_cnx_info['db_name'])
        mysql_engine = create_engine('mysql+mysqldb://{param}'.format(param=param))
        
        return mysql_engine.connect()
     
     # insert case
     @api.model
     def create(vals):
         request = "Insert request based on vals and you mysql table"
         connection = mysql_connect()
         connection.execute(request)
         connection.close()
         return super(ResPartner, self).create(vals)
     
     # update case
     @api.multi
     def write(vals):
         connection = mysql_connect()
         for partner in self:
            request = "update request based on vals, partner.id and your mysql table"
            connection.execute(request)
         connection.close()
         return super(ResPartner, self).write(vals)

     

Best regards

Avatar
Discard
Best Answer

Hi Bejaoui, I have tried your Script and it's working when I use INSERT and DELETE Query. How if I want to use SELECT Query ? How do I use data from Query Result ?

Avatar
Discard