0

I want to browse a many2many field but got this error:

File "C:\Program Files (x86)\OpenERP 7.0\Server\server\openerp\addons\schoolem\schoolem_dispense_cours.py", line 39, in create AttributeError: '**browse_record_list' object has no attribute

heure_cours_dispense_id

This is the code :

class schoolem_dispense_cours(osv.Model):

_name = 'schoolem.dispense_cours'
_columns = {
    'name' : fields.char('Code',required=True,help="Champ automatique"),
    'cours_id' : fields.many2one('schoolem.cours','Nom du Cour',required=True),
    'aca_id' : fields.many2one('schoolem.aca','Annee Academique',required=True),
    'enseignant_id' : fields.many2one('res.users','Enseignant',required=True),
    'salle_de_classe_id' : fields.many2one('schoolem.salle_de_classe','Salle de classe',required=True),
    'heure_cours_id' : fields.many2many('schoolem.heure_cours_dispense','schoolem_dispense_cours_heure_cours_dispense','dispense_cours_id','heure_cours_dispense_id','Heures de cour'),
    #'heure_cours_id' : fields.many2many('schoolem.heure_cours_dispense',required=True),
} 

def create(self, cr, uid, vals, context=None):
        if not vals['heure_cours_id'][0][2]:
            raise osv.except_osv(('Erreur!'), ('Une Programmation doit avoir au moins une heure de cours!'))
        ids = self.search(cr,uid,[('aca_id','=',vals['aca_id'])])
        created_hc = []
        if ids : 
            for id in ids :
                obj_disp_crs = self.pool.get('schoolem.dispense_cours').browse(cr,uid,id)
                created_hc.append(obj_disp_crs.heure_cours_id.heure_cours_dispense_id)
Julius A
  • 38,062
  • 26
  • 74
  • 96
levolutionniste
  • 424
  • 1
  • 6
  • 16

1 Answers1

1

In many2many fields, we can add multiple records. So if we try to browse the many2many field, we will get multiple records or a browselist. You can achieve the same functionality as:

def create(self, cr, uid, vals, context=None):
    ids = self.search(cr,uid,[('aca_id','=',vals['aca_id'])], context=context)
    created_hc = []
    for obj_disp_crs in self.browse(cr, uid, ids, context=context):
        created_hc.extend([x.heure_cours_dispense_id for x in obj_disp_crs.heure_cours_id])

But this code also has another problem because, after the execution of this create function a new record which satisfies the condition aca_id = vals['aca_id'] will be created. But you will not get this record id in the search condition

ids = self.search(cr,uid,[('aca_id','=',vals['aca_id'])], context=context)

because this record is created after the execution of this create function. So in order to get this id also, you need to call the super of the create function first and then execute the further functionality.

Prajul P T
  • 456
  • 3
  • 5
  • got this error : AttributeError: "Field 'heure_cours_dispense_id' does not exist in object 'browse_record(schoolem.heure_cours_dispense, 1)'" – levolutionniste Nov 03 '13 at 18:44
  • `created_hc.extend([x.id for x in obj_disp_crs.heure_cours_id])` works because the field _heure_cours_dispense_id_ is declare in the many2many creation but not in the class reference (schoolem.heure_cours_dispense) while **id** is always defined – levolutionniste Nov 03 '13 at 19:28
  • Ok.... I thought heure_cours_dispense_id is some field in related many2many object. If you want the id, then created_hc.extend([x.id for x in obj_disp_crs.heure_cours_id]) will work... – Prajul P T Nov 04 '13 at 05:11