2017-04-18 58 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