Odoo Help


unicity constraint on a many2many field in openerp

Romuald Franck
on 11/1/13, 4:35 PM 3,087 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.

Joeri Belis
On 8/28/15, 9:07 AM

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 = [
"unique constraint heure_cours_id.",


About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

0 follower(s)


Asked: 11/1/13, 4:35 PM
Seen: 3087 times
Last updated: 8/28/15, 9:10 AM