How to use search_count() method in
Odoo 18
Enterprise
Enterprise
Introduction
In this slide, we’ll discuss on how to use search_count() method
in Odoo 18. In Odoo, we can count records using the
search_count method on an Odoo model. This method returns
the number of records that match a given domain (filter).
search_count is used to count records based on certain
conditions and is commonly used when we need to know how
many records match a set of criteria without actually retrieving
the records themselves. This is particularly useful for
performance optimization since counting records is generally
faster and less resource-intensive than fetching them.
Enterprise
In our custom module, we have a smart tab called student where
all the students having class teacher as this record can be seen.
Along with the name, we can include the number of records with it.
Enterprise
To include the number of records each teacher have, we can make
use of the search_count() method in odoo. For that first we can
define a field to compute the number of records.
student_count = fields.Integer(string='Student count',
compute='_compute_student_count')
After defining the field, we can define the compute function
def _compute_student_count(self):
for rec in self:
rec.student_count =
self.env['student.detail'].search_count([('teacher_id', '=',
rec.id)])
Enterprise
In the compute function we have used the search_count() method to
show the number of records satisfying the domain we provided.
After that we can add the student_count field in the view.
<button class="oe_stat_button" type="object"
name="action_student" string="Student"
icon="fa-people">
<field name="student_count" widget="statinfo"/>
</button>
Enterprise
After adding the field in view and upgrading the module we can see
the count of records is added to the smart button.
Enterprise
If we click on the smart button, it will display the records matching
the domain.
For More Info.
Check our company website for related blogs
and Odoo book.
Check our YouTube channel for
functional and technical videos in Odoo.
Enterprise
www.cybrosys.com

How to use search_count() method in Odoo 18

  • 1.
    How to usesearch_count() method in Odoo 18 Enterprise
  • 2.
    Enterprise Introduction In this slide,we’ll discuss on how to use search_count() method in Odoo 18. In Odoo, we can count records using the search_count method on an Odoo model. This method returns the number of records that match a given domain (filter). search_count is used to count records based on certain conditions and is commonly used when we need to know how many records match a set of criteria without actually retrieving the records themselves. This is particularly useful for performance optimization since counting records is generally faster and less resource-intensive than fetching them.
  • 3.
    Enterprise In our custommodule, we have a smart tab called student where all the students having class teacher as this record can be seen. Along with the name, we can include the number of records with it.
  • 4.
    Enterprise To include thenumber of records each teacher have, we can make use of the search_count() method in odoo. For that first we can define a field to compute the number of records. student_count = fields.Integer(string='Student count', compute='_compute_student_count') After defining the field, we can define the compute function def _compute_student_count(self): for rec in self: rec.student_count = self.env['student.detail'].search_count([('teacher_id', '=', rec.id)])
  • 5.
    Enterprise In the computefunction we have used the search_count() method to show the number of records satisfying the domain we provided. After that we can add the student_count field in the view. <button class="oe_stat_button" type="object" name="action_student" string="Student" icon="fa-people"> <field name="student_count" widget="statinfo"/> </button>
  • 6.
    Enterprise After adding thefield in view and upgrading the module we can see the count of records is added to the smart button.
  • 7.
    Enterprise If we clickon the smart button, it will display the records matching the domain.
  • 8.
    For More Info. Checkour company website for related blogs and Odoo book. Check our YouTube channel for functional and technical videos in Odoo. Enterprise www.cybrosys.com