在表单加载odoo 8上自动填充on2many字段

时间:2016-08-10 05:57:58

标签: one-to-many odoo-8 autofill

我尝试使用type =“one2many”创建一个功能字段并自动填写表单加载。我试过下面的代码:

代码1:

'flat_members1': fields.function(_get_flat_members, relation="family.info", method=True, type="one2many", multi='flat_fkk'),

def _get_flat_members(self, cr, uid, ids, name, arg, context=None):
    cr.execute("Select * from family_info where flat="+str(flat_id)+"")
    cr_res = cr.dictfetchall()
    res = {}
    for data in self.browse(cr,uid,ids):
        res[data.id] = self.pool.get('family.info').search(cr,uid,[('flat', '=', flat_id)])
    return values

代码2:

    member_ids = []
    for res in cr_res:
         member_ids.append((0,0,{'name':res.get('name'),
                               'flat':res.get('flat'),

                               }))
    values.update(family_members1=member_ids)
    return values

两种方式都有错误:

    AttributeError: 'list' object has no attribute 'iteritems'

请给我一个解决方案谢谢。

1 个答案:

答案 0 :(得分:0)

使用Odoo8 new api:

flat_members1 = fields.One2many(compute='_get_flat_members',
                             comodel_name='family.info',
                             string='flat_members1',
                             store=True)


@api.one
@api.depends('flat_id')
def _get_flat_members(self):
    member_ids = []

    # get member_ids

    self.flat_members1 = member_ids