Skip to Content
Menu
This question has been flagged
1 Reply
7152 Views

Hi to everyone. I have a many2many filed on which i would like to put a unique constraint. The first class is :

class schoolem_heure_cours_dispense(osv.Model):

_name = 'schoolem.heure_cours_dispense'
_order = 'name'
_columns = {
    'name' : fields.char('Nom',required=True,help="Champ automatique"),
    'heure_debut' : fields.float('Heure debut',required=True,help="Respecter le format 00:00"),
    'heure_fin' : fields.float('Heure fin',required=True,help="Respecter le format 00:00"),
    'jour_semaine' : fields.selection([('Lundi','Lundi'),('Mardi','Mardi'),('Mercredi','Mercredi'),('Jeudi','Jeudi'),('Vendredi','Vendredi')],'Jour',required=True),
}

_sql_constraints = [('heure_cours_dispense_uniq','unique(heure_debut,heure_fin,jour_semaine)', 'Une heure de cours avec ces meme valeur existe deja!')]

The second class which causes problem is :

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),
}

_sql_constraints = [
    ('dispense_cours_ens_hcours_uniq','unique(aca_id,heure_cours_id,enseignant_id)', "Un enseignant ne peut dispenser deux cours a la meme heure pour cette meme annee academique!"),
    ('dispense_cours_salle_hcours_uniq','unique(aca_id,heure_cours_id,salle_de_classe_id)', "Un cour a deja ete programme dans cette salle de classe a la meme heure pour cette meme annee academique!")
]

But any of these last 2 constraints are not created in the Database. I guess because the field "heure_cours_id" is a many2many and stored in another table not inside the table "schoolem_dispense_cours".

Please I can I implement these constraints. I thought to a create and write test but don't know how to check if a set (heure_cours_id) contains at leat one value in another set (comming from the form vals) in a domain restriction inside a search method for example.

Thank you in advance.

Avatar
Discard
Best Answer

You could add a custom constraint to make the many2many field unique.


Code example:


from osv import osv, fields

class schoolem_dispense_cours(osv.osv):
_name = 'schoolem_dispense_cours'
_inherit = 'schoolem_dispense_cours'

def is_heure_cours_id_unique(self, cr, uid, ids, heure_cours_id, context=None):
for h in heure_cours_id:
if len(self.search(cr,uid,[('heure_cours_id','in',h.id)])) > 1:
return False
return True

def _check_heure_cours_id_on_schoolem_heure_cours_dispense(self, cr, uid, ids, context=None):
for schoolem_dispense_cour in self.browse(cr, uid, ids):
if schoolem_dispense_cour.heure_cours_id:
return self.is_heure_cours_id_unique(cr, uid, ids, schoolem_dispense_cour.heure_cours_id) return True

_constraints = [
(_check_heure_cours_id_on_schoolem_heure_cours_dispense,
"unique constraint heure_cours_id.",
['heure_cours_id']),
]

schoolem_dispense_cours()


Avatar
Discard
Related Posts Replies Views Activity
1
Jun 17
6228
1
Feb 24
836
0
Jul 24
1502
0
Jul 22
60
1
Jul 22
1406