Odoo Help


make a one2many field required

Romuald Franck
on 10/24/13, 5:45 PM 4,451 views

PLease, how to make a one2many field required at least one element in openerp.

'coefficient_ids' : fields.one2many('schoolem.coefficient','cours_id','Coefficients',required=True),

I don't believe you can, offhand. one2many records are just an aggregation of many2one records are the other object. That means the many2one record must be set first. You should try working around that by inheriting the create() and write() functions and raising an error if you attempt to save the record without any values being stored in that field.

Brett Lehrer
on 10/24/13, 7:50 PM

I have rewritten create and write methods do apply the constraints. For others, look here: http://stackoverflow.com/questions/19575157/make-a-one2many-field-required-in-openerp

Romuald Franck
on 10/31/13, 4:54 AM
Daniel Mattos
On 4/17/17, 4:46 PM

class ModelX(models.Model):

    flag_childs = fields.Char('Label XXX', compute='_compute_flag_childs')


    def _compute_flag_childs(self):

        for record in self:

             record.flag_childs = record.child_ids.ids and len(record.child_ids.ids) or ''

Mohamed Essakali
On 1/18/16, 6:49 PM

This old link can help any body facing same problem. \https://code.launchpad.net/~therp-nl/openerp-web/7.0-lp1013636-x2m_honour_required_attribute

On 10/24/13, 7:50 PM

You can add method similar like this (example in idea module):

def _check_name(self,cr,uid,ids):
    for idea in self.browse(cr, uid, ids):
       if 'spam' in idea.name: return False # Can't create ideas with spam!
           return True

...but instead would check the coefficient_ids field that something was passed.

Then add to constraints (for validation):

  _constraints = [(_check_name, 'Please avoid spam in ideas !', ['name'])]


This would be validation for other field types, I'm not exactly sure for one2many or many2one validation. I believe this maybe possible only for many2one.

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: 10/24/13, 5:45 PM
Seen: 4451 times
Last updated: 4/17/17, 5:24 PM