TypeError:'int'对象在odoo中不可迭代

Posted

技术标签:

【中文标题】TypeError:\'int\'对象在odoo中不可迭代【英文标题】:TypeError: 'int' object is not iterable in odooTypeError:'int'对象在odoo中不可迭代 【发布时间】:2016-06-22 13:21:31 【问题描述】:

全部

向表中添加数据时收到错误:

2016-06-22 12:14:59,292 21364 ERROR odoov8 openerp.addons.webvisitorcalc.controllers: prevouseURLsearchEngine: 'search_marker': u'q', 'id': 1, 'uniq_part_in_host': u'google'
2016-06-22 12:14:59,292 21364 ERROR odoov8 openerp.addons.webvisitorcalc.controllers: search_engine_id: 1
2016-06-22 12:14:59,293 21364 ERROR odoov8 openerp.http: Exception during JSON request handling.
Traceback (most recent call last):
  File "/home/skif/odoo/openerp/http.py", line 540, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/home/skif/odoo/openerp/http.py", line 577, in dispatch
    result = self._call_function(**self.params)
  File "/home/skif/odoo/openerp/http.py", line 313, 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 310, in checked_call
    return self.endpoint(*a, **kw)
  File "/home/skif/odoo/openerp/http.py", line 806, in __call__
    return self.method(*args, **kw)
  File "/home/skif/odoo/openerp/http.py", line 406, in response_wrap
    response = f(*args, **kw)
  File "/home/skif/odoo/my-modules/webvisitorcalc/controllers.py", line 179, in register_session
    writeRelationSession.write('search_engine_id':int(1))
  File "/home/skif/odoo/openerp/api.py", line 266, in wrapper
    return new_api(self, *args, **kwargs)
  File "/home/skif/odoo/openerp/models.py", line 3788, in write
    self._write(old_vals)
  File "/home/skif/odoo/openerp/api.py", line 266, in wrapper
    return new_api(self, *args, **kwargs)
  File "/home/skif/odoo/openerp/api.py", line 592, in new_api
    result = method(self._model, cr, uid, self.ids, *args, **old_kwargs)
  File "/home/skif/odoo/openerp/models.py", line 3816, in _write
    for wtuple in vals[field]:
TypeError: 'int' object is not iterable

我如何才能理解这是关系错误。其他关系运作良好。

我的模型文件: [models.py]

class SessionVisitor(models.Model):
        _name = 'webvisitorcalc.session_visitor'
        client_addr = fields.Char(string="Client IP", required=True)
        date_visit = fields.Datetime(default=fields.Datetime.now())
        date_last_check = fields.Datetime(default=fields.Datetime.now())

        target_url_ids = fields.Many2one('webvisitorcalc.url_list', string='Target URL')

        site_trip_id = fields.One2many('webvisitorcalc.site_trip', 'session_ids', string='Trip records')

        #search
        search_engine_id = fields.One2many('webvisitorcalc.search_engine', 'session_ids', string='Search engine')
        search_phrase_id = fields.One2many('webvisitorcalc.search_phrases', 'session_ids', string='Search phrase')

        # advert
        advert_company_id = fields.One2many('webvisitorcalc.advert_company', 'session_ids', string='Advert. company')
        advert_engine_id = fields.One2many('webvisitorcalc.advert_engine', 'session_ids', string='Advert. engine')


class SearchEngine(models.Model):
        _name = 'webvisitorcalc.search_engine'
        name = fields.Char(string="Engine name", required=True)
        uniq_part_in_host = fields.Char(string="Uniq part hostname", required=True)
        search_marker = fields.Char(string="marker for search phrase", required=True) # q='' для Google, text='' для Яндекс
        #utm_marker = fields.Char(string="UTM mark name", required=True)
        #url_list_ids = fields.Many2one('webvisitorcalc.url_list', string='Target URL')
        session_ids = fields.Many2one('webvisitorcalc.session_visitor', string='Session IDs')

这是有错误的代码: [controllers.py]

createParam = 
if (prevouseURLsearchEngine!=):
                _logger.error("prevouseURLsearchEngine: %r", prevouseURLsearchEngine)
                _logger.error("search_engine_id: %r", prevouseURLsearchEngine['id'])
                createParam['search_engine_id'] = int(prevouseURLsearchEngine['id'])
                writeRelationSession.write(createParam)

你能提出我的错误吗?

【问题讨论】:

从堆栈跟踪看来,vals[field] 是一个 int。你期望它是什么? 此字段为:search_engine_id(SessionVisitor 类)。这是关系 SessionVisitorSearchEngine。我在这个字段 id 引擎机器中为来自 google 或 yandex 的访问者会话编写。 在 search_engine_id 行试试这个:createParam['search_engine_id' = [int(prevouseURLsearchEngine['id'])] 现在错误是“TypeError:'int'对象没有属性'____getitem____'” 【参考方案1】:

这是我会尝试的:

createParam = 
if prevouseURLsearchEngine:
    _logger.error("prevouseURLsearchEngine: %r", prevouseURLsearchEngine)
    _logger.error("search_engine_id: %r", prevouseURLsearchEngine['id'])
    # the search_engine field is a `OneToMany` so we are expecting an array not a scalar
    createParam['search_engine_id'] = [ int(prevouseURLsearchEngine['id']) ]
    writeRelationSession.write(createParam)

【讨论】:

我收到另一个错误:文件“/home/skif/odoo/my-modules/webvisitorcalc/controllers.py”,第 179 行,在 register_session writeRelationSession.write(createParam) 文件“/home/skif/odoo/openerp/models.py”,第 3932 行,在 _write result += self._columns[field].set(cr, self, id , field, vals[field], user, context=rel_context) or [] File "/home/skif/odoo/openerp/osv/fields.py", line 796, in set if act[0] == 0: TypeError:“int”对象没有属性“____getitem____” 我的关系可能有误吗?我想将 SearchEngine 模型中的 id 写入 SessionVisitor 模型。对于所有记录 SessionVisitor search_engine_id 不是唯一的,这必须是一个数字。任何记录都可能有相同的数据。我认为在 SearchEngine 中必须是 session_ids = fields.Many2one('webvisitorcalc.session_visitor', string='Session IDs') 并且在 SessionVisitor 中必须是 search_engine_id = fields.One2many('webvisitorcalc.search_engine ', 'session_ids', string='搜索引擎')。我说的对吗?【参考方案2】:

感谢大家的回答。 我写错了关系。我班级的关系必须是换地方。

【讨论】:

以上是关于TypeError:'int'对象在odoo中不可迭代的主要内容,如果未能解决你的问题,请参考以下文章

TypeError:无法在 Django 视图函数中解压不可迭代的 int 对象

TypeError:'int'对象在运行多处理时不可调用

TypeError:无法在 Django REST 框架中解压不可迭代的 int 对象

TypeError: 'int' 对象在使用 reduce 和 lambda 时不可调用

TypeError:'int'对象不可迭代,使用 Python 3 [关闭]

Python - TypeError:'int'对象不可迭代