我在odoo中使用fields_view_get方法在表单视图中添加临时字段,但我希望在编辑模式下传递默认值,不幸的是default_get仅在创建模式下工作,有人可以帮助我吗,谢谢。
def fields_view_get(self,view_id=None, view_type='form', toolbar=False, submenu=False):
res = super(inspection, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu)
categories = self.env['inspection.category'].search([])
all_fields = {}
pages = """ """
fields = """ <group string="Categories" style="font-size:13px; color:black;"> """
if view_type == 'form':
xml_code = res['arch']
for c in categories:
all_fields['category_id_' + str(c.id)] = {
'type': 'boolean',
'string': c.name,
how can in pass default value ???????,
}
fields = fields + """ <field name="%s" string="%s"/>"""%(('category_id_' + str(c.id)),c.name)
xml_code = xml_code[:xml_code.find("<notebook/>")]+" "+fields+" "+xml_code[xml_code.find("<notebook/>")+len("<notebook/>"):]
res['fields'] = dict(res['fields'].items() + all_fields.items())
res['arch'] = xml_code
return res
终于找到答案了,我们可以使用read方法来实现
def read(self, cr, uid, ids, fields=None, context=None, load='_classic_read'):
res = super(inspection, self).read(cr, uid, ids, fields, context=context, load=load)
if len(res) == 1:
cr.execute('select id from inspection_category')
categories = [int(c[0]) for c in cr.fetchall()]
for ce in categories:
res[0]['category_id_'+str(ce)] = True
return res