Odoo 10:如何使用 create 方法将值插入 table 中的 many2one 字段?
Odoo 10: How to insert value to a many2one field in table with create method?
我在 models.py 中的 many2one 字段:
commercial_group = fields.Many2one("simcard.simcard")
我想使用 create 方法在此 many2one 字段中添加一个新值。我从使用元素树解析的 SOAP 响应中获得的值:
response=requests.post(self.url,cert=self.cert,data=body,headers=self.headers)
element = ElementTree.fromstring(response.content.decode('utf-8'))
body_el = element.find('{schemas.xmlsoap.org/soap/envelope}Body')
commercial_group=body_el.find('{example.com}commercialGroup')
我想用这个 commercialGroup 字段和其他一些字段创建一条新记录:
record.sudo().create({
"icc": icc.text,
"imsi": imsi.text,
"msisdn": msisdn.text,
"lte_status": lte_status.text,
"life_cycle_status": life_cycle_status.text,
"sim_model": simmodel.text,
"gprs_status": gprsStatus.text,
"consumption_monthly_data_limit": consumption_monthly_data_limit.text,
"consumption_monthly_data_value": consumption_monthly_data_value.text,
"consumption_monthly_data_thrReached": consumption_monthly_data_thrReached.text,
"commercial_group": commercial_group.text,
"country": country.text,
"operator": operator.text
})
http.request.env.cr.commit()
我收到这个错误:
INFO test odoo.sql_db: bad query: INSERT INTO "simcard_simcard" ("id", "consumption_monthly_data_limit", "consumption_monthly_data_thrReached", "msisdn", "country", "lte_status", "consumption_monthly_data_value", "life_cycle_status", "icc", "gprs_status", "sim_model", "operator", "commercial_group", "imsi", "create_uid", "write_uid", "create_date", "write_date") VALUES(nextval('simcard_simcard_id_seq'), '0', '0', '34590169', 'CH', 'false', '0', 'ACTIVE', '89293165951', '2', 'M2M Plug-in', 're', '1GB_dynPool_Plus_LTE', '29782', 1, 1, (now() at time zone 'UTC'), (now() at time zone 'UTC')) RETURNING id
2018-09-19 13:44:27,441 5714 ERROR test odoo.http: Exception during JSON request handling.
Traceback (most recent call last):
File "/Users/anubhavjhalani/odoo10/odoo/http.py", line 642, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/Users/anubhavjhalani/odoo10/odoo/http.py", line 684, in dispatch
result = self._call_function(**self.params)
File "/Users/anubhavjhalani/odoo10/odoo/http.py", line 334, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/Users/anubhavjhalani/odoo10/odoo/service/model.py", line 101, in wrapper
return f(dbname, *args, **kwargs)
File "/Users/anubhavjhalani/odoo10/odoo/http.py", line 327, in checked_call
result = self.endpoint(*a, **kw)
File "/Users/anubhavjhalani/odoo10/odoo/http.py", line 942, in __call__
return self.method(*args, **kw)
File "/Users/anubhavjhalani/odoo10/odoo/http.py", line 507, in response_wrap
response = f(*args, **kw)
File "/Users/anubhavjhalani/odoo10/addons/web/controllers/main.py", line 895, in call_kw
return self._call_kw(model, method, args, kwargs)
File "/Users/anubhavjhalani/odoo10/addons/web/controllers/main.py", line 887, in _call_kw
return call_kw(request.env[model], method, args, kwargs)
File "/Users/anubhavjhalani/odoo10/odoo/api.py", line 689, in call_kw
return call_kw_multi(method, model, args, kwargs)
File "/Users/anubhavjhalani/odoo10/odoo/api.py", line 680, in call_kw_multi
result = method(recs, *args, **kwargs)
File "/Users/anubhavjhalani/odoo10/addons/simcard/models/models.py", line 259, in sync
parse.parseXml(subscriptionDatas)
File "/Users/anubhavjhalani/odoo10/addons/simcard/models/parse.py", line 71, in parseXml
"operator": operator.text
File "/Users/anubhavjhalani/odoo10/odoo/models.py", line 3846, in create
record = self.browse(self._create(old_vals))
File "/Users/anubhavjhalani/odoo10/odoo/models.py", line 3941, in _create
cr.execute(query, tuple(u[2] for u in updates if len(u) > 2))
File "/Users/anubhavjhalani/odoo10/odoo/sql_db.py", line 154, in wrapper
return f(self, *args, **kwargs)
File "/Users/anubhavjhalani/odoo10/odoo/sql_db.py", line 231, in execute
res = self._obj.execute(query, params)
DataError: invalid input syntax for integer: "1GB_dynPool_Plus_LTE"
^
使用 create 方法向 table 中的 many2one 字段插入值的正确方法是什么?
你的方法很好。你的值字典有一个整数字段的值,它显然得到一个非整数值(1GB_dynPool_Plus_LTE
)。因此,您要么对该字段有错误的值,要么您的字段定义错误,要么您必须将您的值解析为整数,例如通过映射。
编辑:
您的字段 commercial_group
是与模型 simcard.simcard
的多对一关系。我不知道该模型的定义,所以我假设如下:
class Simcard(models.Model):
_name = "simcard.simcard"
name = fields.Char()
现在创建 Odoo 需要 ID 或不需要 many2one 字段。因此,您要么为该模型搜索现有记录,要么创建一个记录,要么就让它为空。我的以下示例使用混合。首先我会搜索,如果没有找到,我的代码示例将创建一个 simcard.simcard
记录并稍后使用它。
commercial_group_name = commercial_group.text
simcard = record.env['simcard.simcard'].search(
[('name', '=', commercial_group_name)], limit=1)
if not simcard:
simcard = record.env['simcard.simcard'].create(
{'name': commercial_group_name})
record.sudo().create({
# other values
"commercial_group": simcard.id,
# other values
})
我在 models.py 中的 many2one 字段:
commercial_group = fields.Many2one("simcard.simcard")
我想使用 create 方法在此 many2one 字段中添加一个新值。我从使用元素树解析的 SOAP 响应中获得的值:
response=requests.post(self.url,cert=self.cert,data=body,headers=self.headers)
element = ElementTree.fromstring(response.content.decode('utf-8'))
body_el = element.find('{schemas.xmlsoap.org/soap/envelope}Body')
commercial_group=body_el.find('{example.com}commercialGroup')
我想用这个 commercialGroup 字段和其他一些字段创建一条新记录:
record.sudo().create({
"icc": icc.text,
"imsi": imsi.text,
"msisdn": msisdn.text,
"lte_status": lte_status.text,
"life_cycle_status": life_cycle_status.text,
"sim_model": simmodel.text,
"gprs_status": gprsStatus.text,
"consumption_monthly_data_limit": consumption_monthly_data_limit.text,
"consumption_monthly_data_value": consumption_monthly_data_value.text,
"consumption_monthly_data_thrReached": consumption_monthly_data_thrReached.text,
"commercial_group": commercial_group.text,
"country": country.text,
"operator": operator.text
})
http.request.env.cr.commit()
我收到这个错误:
INFO test odoo.sql_db: bad query: INSERT INTO "simcard_simcard" ("id", "consumption_monthly_data_limit", "consumption_monthly_data_thrReached", "msisdn", "country", "lte_status", "consumption_monthly_data_value", "life_cycle_status", "icc", "gprs_status", "sim_model", "operator", "commercial_group", "imsi", "create_uid", "write_uid", "create_date", "write_date") VALUES(nextval('simcard_simcard_id_seq'), '0', '0', '34590169', 'CH', 'false', '0', 'ACTIVE', '89293165951', '2', 'M2M Plug-in', 're', '1GB_dynPool_Plus_LTE', '29782', 1, 1, (now() at time zone 'UTC'), (now() at time zone 'UTC')) RETURNING id
2018-09-19 13:44:27,441 5714 ERROR test odoo.http: Exception during JSON request handling.
Traceback (most recent call last):
File "/Users/anubhavjhalani/odoo10/odoo/http.py", line 642, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/Users/anubhavjhalani/odoo10/odoo/http.py", line 684, in dispatch
result = self._call_function(**self.params)
File "/Users/anubhavjhalani/odoo10/odoo/http.py", line 334, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/Users/anubhavjhalani/odoo10/odoo/service/model.py", line 101, in wrapper
return f(dbname, *args, **kwargs)
File "/Users/anubhavjhalani/odoo10/odoo/http.py", line 327, in checked_call
result = self.endpoint(*a, **kw)
File "/Users/anubhavjhalani/odoo10/odoo/http.py", line 942, in __call__
return self.method(*args, **kw)
File "/Users/anubhavjhalani/odoo10/odoo/http.py", line 507, in response_wrap
response = f(*args, **kw)
File "/Users/anubhavjhalani/odoo10/addons/web/controllers/main.py", line 895, in call_kw
return self._call_kw(model, method, args, kwargs)
File "/Users/anubhavjhalani/odoo10/addons/web/controllers/main.py", line 887, in _call_kw
return call_kw(request.env[model], method, args, kwargs)
File "/Users/anubhavjhalani/odoo10/odoo/api.py", line 689, in call_kw
return call_kw_multi(method, model, args, kwargs)
File "/Users/anubhavjhalani/odoo10/odoo/api.py", line 680, in call_kw_multi
result = method(recs, *args, **kwargs)
File "/Users/anubhavjhalani/odoo10/addons/simcard/models/models.py", line 259, in sync
parse.parseXml(subscriptionDatas)
File "/Users/anubhavjhalani/odoo10/addons/simcard/models/parse.py", line 71, in parseXml
"operator": operator.text
File "/Users/anubhavjhalani/odoo10/odoo/models.py", line 3846, in create
record = self.browse(self._create(old_vals))
File "/Users/anubhavjhalani/odoo10/odoo/models.py", line 3941, in _create
cr.execute(query, tuple(u[2] for u in updates if len(u) > 2))
File "/Users/anubhavjhalani/odoo10/odoo/sql_db.py", line 154, in wrapper
return f(self, *args, **kwargs)
File "/Users/anubhavjhalani/odoo10/odoo/sql_db.py", line 231, in execute
res = self._obj.execute(query, params)
DataError: invalid input syntax for integer: "1GB_dynPool_Plus_LTE"
^
使用 create 方法向 table 中的 many2one 字段插入值的正确方法是什么?
你的方法很好。你的值字典有一个整数字段的值,它显然得到一个非整数值(1GB_dynPool_Plus_LTE
)。因此,您要么对该字段有错误的值,要么您的字段定义错误,要么您必须将您的值解析为整数,例如通过映射。
编辑:
您的字段 commercial_group
是与模型 simcard.simcard
的多对一关系。我不知道该模型的定义,所以我假设如下:
class Simcard(models.Model):
_name = "simcard.simcard"
name = fields.Char()
现在创建 Odoo 需要 ID 或不需要 many2one 字段。因此,您要么为该模型搜索现有记录,要么创建一个记录,要么就让它为空。我的以下示例使用混合。首先我会搜索,如果没有找到,我的代码示例将创建一个 simcard.simcard
记录并稍后使用它。
commercial_group_name = commercial_group.text
simcard = record.env['simcard.simcard'].search(
[('name', '=', commercial_group_name)], limit=1)
if not simcard:
simcard = record.env['simcard.simcard'].create(
{'name': commercial_group_name})
record.sudo().create({
# other values
"commercial_group": simcard.id,
# other values
})