2017-04-18 65 views
0

尝试从列接收数据时出错。 型号是:错误无法修改'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,但无法收到其他记录字段的数据?

+0

编辑你的问题,并添加生成错误的方法,不要把错误日志放在一个报价中,通过make代码,所以我们可以看到我修改的错误源 – Cherif

+0

。 在调用find_client_in_filial_phone之后,我必须接收记录集模型int_filial_phone。而当Itry接收此记录的ID字段 - 一切正常(int_phone.id)。当我尝试接收此记录的另一个字段(例如int_phone.name)时,我收到错误。 – Skif

回答

1

在新版本的API中,当您使用“搜索”方法时,值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 

如果您的搜索没有找到值,则返回一个空的记录集。

+0

非常感谢您的回答! – Skif