
Odoo中文社区可以通过以下两个域名访问:shine-it.net , odoo.net.cn
由于系统升迁的原因,本论坛部分较早期的内容存在格式和链接损坏失效的问题,并非本论坛系统本身的缺陷,望谅解
本社区没有维护任何QQ群讨论组,任何与本社区同名的QQ群讨论组的言论与本社区无关!
开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号
如果您登录系统碰到问题,请在微信公众号留言:
Odoo10 怎么在onchange里面为one2many赋值?
-
我现在有这样的一个模型
Class A:
fields.one2many('a')Class B:
fields.one2many('c')Class
fields.many2one('product.product')现在的需求是,在 Class A里面有一个onchange方法,他是根据客户的改变,带出他的one2many(这里的one2many数据,包含下级的,都是从其他地方取过来赋值的),,请问这样的数据,怎么操作呢。。
-
one2many的赋值规则是[(0,0,{}),...],比如你的one2many字段为field_c,那么你的onchange方法中赋值方式为:
field_c = [(0, 0, {'name': 'Test', ...})]
-
@萧云飞 这个我现在知道,我现在的model其实是one2many里面还有一个one2many。。。第一个one2many赋值没有问题,但第二个one2many就有问题了
-
@萧云飞 我现在的代码是这样的。
def _materialValue(self): page = [] for item in self.suite_series.main_material_ids: product_ids = [] data = { 'location': item.area_id.id, # 区域位置 'name': item.project_record_id.name, # 项目名称 'model': item.model_type, # 型号 'brand': item.product_brand_id.id, # 品牌 'specifications': item.specifications, # 规格 'price': item.price, # 单价 'number': item.number, # 数量 'unit': item.unit.id, # 单位 # 'product_ids': False, 'total_price': item.total_price, # 合价 'remarks': item.general_description, # 备注 'attribute': item.attribute_value_ids # 属性 } # 遍历材料 0,false,[object Object],0,false,[object Object] for val in item.product_id: line_item = { 'product_id': val.product_id.id, # 材料 'classify': val.pro_type # 分类 } product_ids.append((0, 0, line_item)) data.update({'product_ids': product_ids}) page.append((0, False, data)) return page
返回的数据是:
< type 'list' >: [(0, 0, { 'specifications': u '\u89c4\u683c', 'attribute': product.attribute.value(), 'price': 100.0, 'number': 1, 'remarks': False, 'unit': 8, 'total_price': 10000.0, 'name': u '\u6728\u5730\u677f', 'location': 12, 'model': u '\u578b\u53f7', 'brand': 1, 'product_ids': [(0, 0, { 'product_id': 25, 'classify': False }), (0, 0, { 'product_id': 33, 'classify': False })] }), (0, 0, { 'specifications': u '12', 'attribute': product.attribute.value(), 'price': 237500.0, 'number': 112, 'remarks': False, 'unit': 8, 'total_price': 0.0, 'name': u '\u74f7\u7816', 'location': 12, 'model': u '12', 'brand': 1, 'product_ids': [(0, 0, { 'product_id': 32, 'classify': False }), (0, 0, { 'product_id': 33, 'classify': False })] })]
onchange方法
@api.onchange('suite_series') def onchange_suite_series(self): """套装改变""" if self.main_materials_list or self.basic_materials_list or self.basic_incidentals: return {'warning': { 'title': '警告', 'message': '材料已经存在' }} self.main_materials_list = self._materialValue()
-
@jalena 这个规则是针对一个对象one2many和many2many赋值的,所以你第一层这样赋值没有问题,但是第二层,你的对象是谁,这个数据包,它自己区分么?我想这是一个问题,我没有这样赋值过,你可以在下层对象再写一个onchange,然后在这样赋值,我想就解决了
-
@萧云飞 那我能不能直接new一个对象进去呢
-
@萧云飞 感谢您哈。做出来了,第一层赋值确实如你所说,第二次我就只能重写create方法来写入数据了!
@api.onchange('suite_series') def onchange_suite_series(self): """套装改变""" if self.main_materials_list or self.basic_materials_list or self.basic_incidentals: return {'warning': { 'title': '警告', 'message': '材料已经存在' }} self.basic_incidentals = self._incidentals() self.main_materials_list = self._materialValue() self.basic_materials_list = self._basicMaterial() @api.multi def _materialValue(self): """返回主材数据的model""" page = list() for item in self.suite_series.main_material_ids: # 主材项目数据 data = { 'location': item.area_id.id, # 区域位置 'name': item.project_record_id, # 项目名称 'model': item.model_type, # 型号 'brand': item.product_brand_id.id, # 品牌 'specifications': item.specifications, # 规格 'price': item.price, # 单价 'number': item.number, # 数量 'unit': item.unit.id, # 单位 'total_price': item.total_price, # 合价 'remarks': item.general_description, # 备注 'attribute': item.attribute_value_ids # 属性 } page.append((0, 0, data)) return page
@api.model def create(self, values): """重写保存方法""" result = super(BudgetMainMaterial, self).create(values) old_material = self.env['material'].search([('main_material_id', '=', values[u'name'])]) for material in old_material: self.env['budget.material'].create({'product_id': material.product_id.id, 'main_id': result.id})
-