Comment 4 for bug 356628

Revision history for this message
António Anacleto (antonio-anacleto) wrote : Re: [5.0] fields.related on many2many field always indicate (0)

Hello, I changed the code in the /server/bin/osv/fields.py and solved my problem

this was the function I changed:

    def _fnct_read(self, obj, cr, uid, ids, field_name, args, context=None):
        self._field_get2(cr, uid, obj, context)
        if not ids: return {}
        relation = obj._name
        res = {}.fromkeys(ids, False)
        objlst = obj.browse(cr, uid, ids)
        for data in objlst:
            if not data:
                continue
            t_data = data
            relation = obj._name
            for i in range(len(self.arg)):
                field_detail = self._relations[i]
                relation = field_detail['object']
                try:
                    if not t_data[self.arg[i]]:
                        t_data = False
                        break
                except:
                    t_data = False
                    break
                # Modificado por AMA para resolver o bug em que o one 2 many em related aparece apenas o ultimo valor
                if field_detail['type'] in ('one2many', 'many2many'):
                    all_t_data = t_data[self.arg[i]]
                    t_data_list=[]
                    for t_data in all_t_data:
                        t_data_list.append(t_data.id)
                else:
                    t_data = t_data[self.arg[i]]

            if type(t_data) == type(objlst[0]) and field_detail['type'] not in ('one2many', 'many2many'):
                res[data.id] = t_data.id
            else:
                res[data.id] = t_data

            if field_detail['type'] in ('one2many', 'many2many'):
                if type(t_data) == type(objlst[0]):
                    res[data.id] = t_data_list
                else:
                    res[data.id] = t_data

        if self._type=='many2one':
            ids = filter(None, res.values())
            if ids:
                ng = dict(obj.pool.get(self._obj).name_get(cr, uid, ids, context=context))
                for r in res:
                    if res[r]:
                        res[r] = (res[r], ng[res[r]])
        return res