尝试从列接收数据时出错。 模型是:
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(最近一次调用最后一次):文件 " /home/skif/odoo/openerp/http.py",第648行,在_handle_exception中 return super(JsonRequest,self)._ handle_exception(exception)File" /home/skif/odoo/openerp/http.py" ;,第685行,在发送中 result = self._call_function(** self.params)文件" /home/skif/odoo/openerp/http.py" ;,第321行,在_call_function中 return checked_call(self.db,* args,** kwargs)File" /home/skif/odoo/openerp/service/model.py" ;,第118行,在包装器中 返回f(dbname,* args,** kwargs)文件" /home/skif/odoo/openerp/http.py" ;,第314行,在checked_call中 result = self.endpoint(* a,** kw)文件" /home/skif/odoo/openerp/http.py" ;,第964行,调用 返回self.method(* args,** kw)文件" /home/skif/odoo/openerp/http.py" ;,第514行,在response_wrap中 response = f(* args,** kw)File" /home/skif/odoo/openerp/my-addons/pr_finance/controllers/controllers.py", 第151行,在upload_file中 _logger.error(" PHONE INT:%r",int_phone [0] .name)文件" /home/skif/odoo/openerp/fields.py" ;,第830行,在获得 self.determine_value(record)文件" /home/skif/odoo/openerp/fields.py",第930行,在determine_value中 record._prefetch_field(self)File" /home/skif/odoo/openerp/api.py" ;,第248行,在包装器中 返回new_api(self,* args,** kwargs)文件" /home/skif/odoo/openerp/models.py",第3308行,在_prefetch_field中 result = records.read([fs中f为F的名称],加载=' _classic_write')文件" /home/skif/odoo/openerp/api.py" ;,第248行,在包装中 返回new_api(self,* args,** kwargs)文件" /home/skif/odoo/openerp/models.py" ;,第3238行,正在读取 self._read_from_database(存储,继承)文件" /home/skif/odoo/openerp/api.py" ;,第248行,在包装器中 在_read_from_database中返回new_api(self,* args,** kwargs)文件" /home/skif/odoo/openerp/models.py",第3376行 cr.execute(query_str,params)文件" /home/skif/odoo/openerp/sql_db.py" ;,第141行,在包装器中 return f(self,* args,** kwargs)文件" /home/skif/odoo/openerp/sql_db.py" ;,第220行,执行 res = self._obj.execute(query,params)File" /home/skif/.local/lib/python2.7/site-packages/psycopg2/extensions.py", 第129行,在getquoted中 pobjs = [在self._seq中适应(o)for o]编程错误:无法调整类型' pr_filials.int_filial_phone'
我如何从记录中接收数据?为什么我收到了id但无法从其他记录领域接收数据?
答案 0 :(得分:1)
在api的新版本中,当您使用方法" search"时,返回的值是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)
在您的代码中,您尝试使用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
你必须这样做。
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
如果您的搜索未找到值,则返回空记录集。