Skip to Content
Menu
This question has been flagged
1 Reply
1726 Views
Hello,

I'm trying to connect to a mysql database within a self-written odoo app.

Here is my code:
from odoo import api, fields, models
import mysql.connector

MYSQL_USER = 'root'
MYSQL_PASSWORD = 'root'
MYSQL_HOST = 'localhost'
MYSQL_DATABASE = 'data'

class importlogic(models.Model):
    _name = 'test.data'
    _inherit = 'test.data'

    cnx = None

    def action_import_data(self):
        self.process()

    def process(self):
        self.connect_mysql()

    def connect_mysql(self):
        self.cnx = mysql.connector.connect(user=MYSQL_USER,
                password=MYSQL_PASSWORD,
                host=MYSQL_HOST,
                database=MYSQL_DATABASE)

And I receive the following error message:
AttributeError: 'test.data' object attribute 'cnx' is read-only

For the moment I have no idea why this appears, can someone help me?
Thank you.
Avatar
Discard
Author Best Answer

Okay, I found the problem

should be importlogic.cnx > the classname because it is the class attribute https://www.toptal.com/python/python-class-attributes-an-overly-thorough-guide


Avatar
Discard