错误无法适应类型 'some.model.'

Error can't adapt type 'some.model.'

尝试从列接收数据时出现错误。 型号是:

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"),
    ]

方法:

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

我尝试接收记录 ID:

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

一切正常 当我尝试接收另一个记录字段时:

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

我收到错误:

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'

如何从记录中接收数据?为什么我收到了 id 但收不到来自其他记录字段的数据?

在新版本的api中,当你使用方法"search"时,值return是一个recordSet。

示例:

旧api版本

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)

在新版本中api

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

在您的代码中,您尝试使用记录集进行浏览。

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

你必须这样做。

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

如果您的搜索没有找到值,则空记录集是 return。