Odoo。 TypeError:'int'对象不可迭代

时间:2016-08-04 17:32:09

标签: python orm openerp typeerror odoo-8

我的代码有问题。

class SiteTrip(models.Model):
    _name = 'vips_vc.site_trip'

    name = fields.Char()
    session_ids = fields.One2many('vips_vc.session', 'site_trip_id', string='Session ID', index=True)
    url_prevouse_ids = fields.Many2one('vips_vc.url_list', string='Prevouse URL', index=True)
    url_current_ids  = fields.Many2one('vips_vc.url_list', string='Current URL',  index=True)

class URLList(models.Model):
    _name = 'vips_vc.url_list'

    name = fields.Char(string="URL", required=True)
    url_parametes = fields.Char(string="URL parameters") 

    target_session_id = fields.One2many('vips_vc.session', 'target_url_ids', string='Target URL')

    site_trip_prevouse_id = fields.One2many('vips_vc.site_trip', 'url_prevouse_ids', string='Prevouse URL')
    site_trip_current_id =  fields.One2many('vips_vc.site_trip', 'url_current_ids',  string='Current URL')


    remote_sites_id = fields.One2many('vips_vc.remote_sites', 'site_url_ids', string='Remote site page with URL')
    remote_sites_target_url_id = fields.One2many('vips_vc.remote_sites', 'target_url_ids', string='URL on remote site page')

我的控制器:

def register_trip(self, currentURLid, prevouseURLid, sessionID):
        currentURLid = int(currentURLid)
        prevouseURLid = int(prevouseURLid)
        result = None
### something
            _logger.info("CREATE -----> session_ids: %r url_prevouse_ids: %r url_current_ids: %r",
                          sessionID, prevouseURLid, currentURLid)
            result = table.create({'session_ids': sessionID, 'url_prevouse_ids': prevouseURLid,
                                   'url_current_ids': currentURLid})
### something
        return result.id

错误是:

2016-08-04 17:20:52,931 24261 INFO odoov8 openerp.addons.vips_vc.controllers: CREATE -----> session_ids: 59 url_prevouse_ids: 8 url_current_ids: 1
2016-08-04 17:20:52,938 24261 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/vips_vc/controllers.py", line 194, in register_session
    self.register_trip(currentURLid, prevouseURLid, sessionID)
  File "/home/skif/odoo/my-modules/vips_vc/controllers.py", line 375, in register_trip
    'url_current_ids': currentURLid})
  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 4094, in create
    record = self.browse(self._create(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 508, in new_api
    result = method(self._model, cr, uid, *args, **old_kwargs)
  File "/home/skif/odoo/openerp/models.py", line 4279, in _create
    result += self._columns[field].set(cr, self, id_new, field, vals[field], user, rel_context) or []
  File "/home/skif/odoo/openerp/osv/fields.py", line 795, in set
    for act in values:
TypeError: 'int' object is not iterable

正如您所看到的那样,当我尝试在vips_vc.site_trip中添加记录时,我收到错误。并且仅针对currentURLid出错。它具有整数值。 prevouseURLid也有整数值。 prevouseURLid和currentURLid具有相似的关系One2Many / Many2One。

prevouseURLid正在运作。 currentURLid不是。

在这一行中,我检查了所有param(记录器输出):

2016-08-04 17:20:52,931 24261 INFO odoov8 openerp.addons.vips_vc.controllers: CREATE -----> session_ids: 59 url_prevouse_ids: 8 url_current_ids: 1

url_prevouse_ids和url_current_ids的类型是整数。他们定义了价值。他们有类似的关系。并插入url_current_ids返回错误。 为什么会这样?

明天一切正常!

我没有触及关系。我没有触摸变量类型......

UPD:在所有操作之后我有这个:如果我正在尝试用任何参数创建记录(sessionID,prevouseURLid,currentURLid)我收到同样的错误: TypeError:'int'对象不可迭代

1 个答案:

答案 0 :(得分:0)

我发现了错误。 我不知道它是如何早期工作的......

result = table.create({'session_ids': sessionID, 'url_prevouse_ids': prevouseURLid,
                                   'url_current_ids': currentURLid})

此处显示其他表(模型)的ID记录。当我删除所有数据(模块被删除并再次安装)。 在那之后,我逐步检查所有数据接收到的变量并存储在DB中。我发现vips_vc.url_list和vips_vc.session没有数据。 之后我在创建所有记录之后放置了这样的代码:

_logger.info(".....> Commit record ID %r", result.id)
table.env.cr.commit()

我不知道为什么代码在没有commit()的情况下提前工作。