Odoo Help


How To prevent adding more than X items into One2Many

Ameen Rabea
on 2/28/16, 10:39 AM 521 views

I have two models Parent and Child.  In the Parent form I am allowing the user to adding Childs based on the One2Many relation between Parent and Child. Then I need to prevent him from adding more than X number of Childs, how to achieve


I have two models Parent and Child. In the Parent form I am allowing the user to add Childs based on the One2Many relation between Parent and Child. Then I need to prevent from adding more than X number of Childs, how to achieve this?

Right now I am able to check the number of added childs using OnChange(). and the alert message appears to the user that he acceded the limit of childs.

Assume the limit is 3 childs, how can I do the following:

  1. After adding the 4 childs, show alert message (this already done),  then delete the last added child(s) that acceds the limit

  2. Or, once childs max limit achieved, hide the "Add an Item" link


Hi Abu Faisal,

Should you create a pythonic constrain @api.constrains('One2many') and do a check on One2many field length using len() and raise warning.

Something like this, https://github.com/odoo/odoo/blob/9.0/addons/account/models/account_invoice.py#L1236

len(self.One2many_field) would give you records number and then you can raise warning.

Hi Rifakat, Thanks for helping me ;-) I successfully implemented the constrain, and now the user cannot save the submitted parent form if the child items accedes the limit. User should delete the extra child items manually. As you know the @api.constrain function called on form save, so do you know if I can even prevent adding more items before reaching the form saving? (I am able to show the warning after he adds but not able to delete the added child rows)

Ameen Rabea
on 2/29/16, 2:48 AM

you right. constrain will be checked when saving the record while user has already added all items. you can also create @api.onchange('order_line'), to raise a warning, but as you may know, onchange would only raise warning and will not restrict user to add more items. In your case you will need @api.constrain() as well as @api.onchange(), constrain to restrict saving records, and onchange to warn user that he is doing wrong. Thanks.

Rifakat Haradwala (rha)
on 2/29/16, 7:55 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

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

1 follower(s)


Asked: 2/28/16, 10:39 AM
Seen: 521 times
Last updated: 2/29/16, 1:39 AM