Odoo Help


Count on2many records

Abdullah Sofan
on 12/8/14, 2:48 PM 5,726 views

Is there a way to count the number of records in a one2many field? 

@abdullah What exactly you want to achieve by knowing the count of one2many fields?

Emipro Technologies Pvt. Ltd.
on 12/8/14, 3:06 PM

I have two classes : (batches) & (batches.registrations) In (batches), I have a one2many field called "student_registrations" pointing to (batches.registrations) and showing the student registrations for this batch. However, I can only register 18 to 20 students as per the capacity of the classroom. I need to know how to limit the number of student registrations per batch in the one2many field once the registrations reach 18 or 20.

Abdullah Sofan
on 12/9/14, 12:56 AM

Related topic link https://www.odoo.com/forum/help-1/question/how-set-limit-for-number-records-of-x2many-6627 https://www.odoo.com/forum/help-1/question/how-to-limit-the-total-number-values-for-a-one2many-field-49700

on 12/9/14, 1:38 AM

Atchuthan - Technical Consultant, Sodexis Inc

--Atchuthan - Technical Consultant, Sodexis Inc--
| 5 5 8
Puducherry, India
--Atchuthan - Technical Consultant, Sodexis Inc--
Passionate coder in Python/Odoo(formerly known as OpenERP).

Contact me via GMail: atchuthantu@gmail.com via Skype: atchuthan_skype
via Linkedin: http://in.linkedin.com/in/atchuthantu
Atchuthan - Technical Consultant, Sodexis Inc
On 12/9/14, 2:04 AM

Try using _constraints.

    def _check_batch_limit(self, cursor, user, ids, context=None):
        for record in self.browse(cursor, user, ids, context=context):
            //limit is set as 18 or 20
            if len(record.student_registrations) > record.limit:
                return False
        return True

    _constraints = [
        (_check_batch_limit, 'Error:Limit reached.', ['student_registrations']),

I am sorry for my question... but does this code get the limit value from a field in the form?

Abdullah Sofan
on 12/9/14, 11:43 PM

This code does not work. Why did you accept the answer?

Abdullah Sofan
on 12/10/14, 12:29 AM

@Abdullah, No one will provide you exact code changes for your problem. We can only provide how this can be achieved. and VOTE is different from ACCEPT.

Atchuthan - Technical Consultant, Sodexis Inc
on 12/10/14, 12:53 AM

yes. I obtained the limit value from "LIMIT" field at FORM view.

Atchuthan - Technical Consultant, Sodexis Inc
on 12/10/14, 12:55 AM

Sorry for my previous comments. You were absolutely right from the start. Thanks you Atchuthan.

Abdullah Sofan
on 12/22/14, 11:59 AM



| 5 4 6
Zagreb | Karlovac, Croatia

there is only 10 kind of people the ones that understand binary ... and others

On 12/9/14, 3:59 AM

use len like.. 

so = self.pool.get('sale.order').browse(cr, uid, id)
count = len(so.order_line)


hope it helps

edit after comment:

also need to check if there is an object like:
count = so.order_line and len(so.order_line) or 0

I tried this but I get this error: object of type 'bool' has no len()

Abdullah Sofan
on 12/9/14, 6:52 AM

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

3 follower(s)


Asked: 12/8/14, 2:48 PM
Seen: 5726 times
Last updated: 3/16/15, 8:10 AM