0
votes

I have error when try to receive data from column. Model is:

class int_filial_phone(models.Model):
    _name = 'pr_filials.int_filial_phone'

    name = fields.Char(string="Partner-number") #,  compute='_get_name_field')
    number = fields.Char(string="Phone")
    active = fields.Boolean(string="Active")
    filial_addr_ids = fields.One2many('pr_filials.filial_addr', 'int_filial_phone_id', string='Address')
    filial_id = fields.Many2one('res.company',  string='Filial')
    advert_phone_ids = fields.One2many('pr_filials.advert_phone', 'int_filial_phone_id', 'Advert phone')

    _sql_constraints = [
        ('number_unique',
         'UNIQUE(number)',
         "The parameter number must be unique"),
    ]

Methods:

def find_client_in_filial_phone(self, phone, table):
        cr, uid, context, registry = request.cr, request.uid, request.context, request.registry
        result = None
        table = request.env[table]
        phone = self.format_symbol_phone(phone)
        _logger.error('find_client_in_filial_phone phone: %r ', phone )
        ids = table.sudo().search([['number', '=', phone],], limit=1)
        if(len(ids)>0):
            result = table.sudo().browse(ids)[0]
        _logger.error('find_client_in_filial_phone result: %r ', result )
        return result

I try to receive record id:

int_phone = self.find_client_in_filial_phone(data[3], 'pr_filials.int_filial_phone')
int_phone_id = int(int_phone.id)

All work fine When i try to receive another field of record:

_logger.error("PHONE NAME: %r", int_phone[0].name)

I receive error:

Traceback (most recent call last): File "/home/skif/odoo/openerp/http.py", line 648, in _handle_exception return super(JsonRequest, self)._handle_exception(exception) File "/home/skif/odoo/openerp/http.py", line 685, in dispatch result = self._call_function(**self.params) File "/home/skif/odoo/openerp/http.py", line 321, in _call_function return checked_call(self.db, *args, **kwargs) File "/home/skif/odoo/openerp/service/model.py", line 118, in wrapper return f(dbname, *args, **kwargs) File "/home/skif/odoo/openerp/http.py", line 314, in checked_call result = self.endpoint(*a, **kw) File "/home/skif/odoo/openerp/http.py", line 964, in call return self.method(*args, **kw) File "/home/skif/odoo/openerp/http.py", line 514, in response_wrap response = f(*args, **kw) File "/home/skif/odoo/openerp/my-addons/pr_finance/controllers/controllers.py", line 151, in upload_file _logger.error("PHONE INT : %r", int_phone[0].name) File "/home/skif/odoo/openerp/fields.py", line 830, in get self.determine_value(record) File "/home/skif/odoo/openerp/fields.py", line 930, in determine_value record._prefetch_field(self) File "/home/skif/odoo/openerp/api.py", line 248, in wrapper return new_api(self, *args, **kwargs) File "/home/skif/odoo/openerp/models.py", line 3308, in _prefetch_field result = records.read([f.name for f in fs], load='_classic_write') File "/home/skif/odoo/openerp/api.py", line 248, in wrapper return new_api(self, *args, **kwargs) File "/home/skif/odoo/openerp/models.py", line 3238, in read self._read_from_database(stored, inherited) File "/home/skif/odoo/openerp/api.py", line 248, in wrapper return new_api(self, *args, **kwargs) File "/home/skif/odoo/openerp/models.py", line 3376, in _read_from_database cr.execute(query_str, params) File "/home/skif/odoo/openerp/sql_db.py", line 141, in wrapper return f(self, *args, **kwargs) File "/home/skif/odoo/openerp/sql_db.py", line 220, in execute res = self._obj.execute(query, params) File "/home/skif/.local/lib/python2.7/site-packages/psycopg2/extensions.py", line 129, in getquoted pobjs = [adapt(o) for o in self._seq] ProgrammingError: can't adapt type 'pr_filials.int_filial_phone'

How I can receive data from record? Why i received id but cannot received data from other fields of record?

1
edit you question and add the method that generate the error and don't put the error log inside a quote by make code so we can see the error sourceCharif DZ
I modified. After calling find_client_in_filial_phone I must receive recordset model int_filial_phone. And when Itry to receive ID field of this record - all fine(int_phone.id). When I try to receive another field this record (for example int_phone.name) I receive error.Skif

1 Answers

2
votes

In the new version of the api, when you used the method "search", the value return is a recordSet.

Example:

Old api version

record_ids = self.pool.get('model.name').search([('name', '=', 'Jon doe')])
# The value of record_ids is like [1,2,3,4]
records = self.pool.get('model.name').browse(records_ids)
# The value of records is like model.name(1,2,3,4)

In the new version of api

records = self.env['model.name'].search([('name', '=', 'Jondoe')])
# The vale of records is like model.name(1,2,3,4)

In your code you try to browse with recordSet.

def find_client_in_filial_phone(self, phone, table):
    ...
    ids = table.sudo().search([['number', '=', phone],], limit=1)
    # Here ids is a recordSet
    if(len(ids)>0):
        result = table.sudo().browse(ids)[0]
    _logger.error('find_client_in_filial_phone result: %r ', result )
    return result

You must do like this.

def find_client_in_filial_phone(self, phone, table):
    cr, uid, context, registry = request.cr, request.uid, request.context, request.registry
    table = request.env[table]
    phone = self.format_symbol_phone(phone)
    _logger.error('find_client_in_filial_phone phone: %r ', phone )
    result = table.sudo().search([['number', '=', phone],], limit=1)
    _logger.error('find_client_in_filial_phone result: %r ', result )
    return result

If your search doesn't find value, an empty recordSet is return.