XML RPC 中Many2many字段值写入postgres数据库
-
python 代码:
class PartFamilyBase(models.Model): _name = 'part.family' _description = 'Part Family' name = fields.Char('Name', required=True) code = fields.Char('Code', required=True) description = fields.Char('Description') value_drive_ids = fields.Many2many('part.value.drive', string='Default Value Drive') parent_id = fields.Many2one('part.family', string='Parent Famlily')
数据库中字段:
导入脚本处理的结果:
vals = { 'code': j[0], 'name': j[1], 'parent_id': p_id, 'value_drive_ids': (0, 0, j[3]) } print(vals) #{'parent_id': '', 'value_drive_ids': (0, 0, [18, 19]), 'code': 'ABB001', 'name': 'Industrial'} new_id = models.execute_kw(db, uid, password, 'part.family', 'create', [vals]) print(new_id)
问题:
1.python 代码中的多对多字段可以加唯一约束吗?
2.代码中的变量名为value_drive_ids,是Many2many类型的;数据库中为value_drive_id,且是Integer类型的,这样表示正确吗?
3.XML RPC把多对多数据写入数据库时,其他字段值都能写入成功,只有多对多的字段写入不成功,是格式错误,还是什么,希望提出可以成功写入的方法? -
https://www.odoo.com/documentation/10.0/reference/orm.html
Many2many field; the value of such a field is the recordset.
Parameters
comodel_name -- name of the target model (string)
The attribute comodel_name is mandatory except in the case of related fields or field extensions.Parameters
relation -- optional name of the table that stores the relation in the database (string)
column1 -- optional name of the column referring to "these" records in the table relation (string)
column2 -- optional name of the column referring to "those" records in the table relation (string)The attributes relation, column1 and column2 are optional. If not given, names are automatically generated from model names, provided model_name and comodel_name are different!
Parameters
domain -- an optional domain to set on candidate values on the client side (domain or string)
context -- an optional context to use on the client side when handling that field (dictionary)
limit -- optional limit to use upon read (integer)Many2many 是存在中间表里的
-
@萧云飞
1.比如写的约束,我修改原先的约束后,数据库是增加了一个新的约束,原先的约束并没有自动删除的,这个是什么原因?
2.多对多的我value_drive_ids写为[(0, 0, [18, 19])],Many2many的那张关系表不会自动写入对应关系的.
3.Many2many写入时,跟写入数据的先后顺序有关吗,比如这里是part.family和part.value.drive模块,我先写入part.value.drived的数据,再次写入part.family的数据,同时把value_drive_ids写入.反之依然.但是都不能关系表数据都写入不成功.感谢您的帮助.