可以在context里面,增加many2one字段,这样后台通过或缺context,可以得到尚未保存到数据库临时数据
如果是嵌套在field里面的关系字段,也可以通过parent关键字获取父项的字段信息
xml code如下
<xpath expr="//page[@name='variants']/field[@name='attribute_line_ids']" position="attributes">
<attribute name="context">{'attribute_line_ids':attribute_line_ids}</attribute>
</xpath>
<xpath expr="//field[@name='value_ids']" position="attributes">
<attribute name="options">{'no_create_edit': True,'limit':2}</attribute>
</xpath>
<xpath expr="//field[@name='attribute_id']" position="attributes">
<attribute name="context">{'attribute_line_ids':parent.attribute_line_ids}</attribute>
</xpath>
Python代码如下
#利用上下文获取tree视图里面未保存的记录
#write_log(str(self._context))
attr_ids = self._context.get('attribute_line_ids',[])
valid_attribute_ids = []
for item in attr_ids:
#write_log(str(item))
if item[0] > 0:
#[4, 25, False]这种格式的记录,是已经存在的属性,其中25是attribute_line_id
valid_attribute_ids.append(self.env['product.template.attribute.line'].browse(item[1]).attribute_id.id)
else:
#记录格式[0, 'virtual_1087', {'product_tmpl_id1': 18, 'attribute_id': 1, 'value_ids': [[6, False, [1]]]}]
if item[2]['attribute_id']:
valid_attribute_ids.append(item[2]['attribute_id'])
self.set_up_domain(domain,'id','not in',valid_attribute_ids)