Skip to Content
Menu
This question has been flagged
1 Reply
1742 Views

I've made a model with a field name Car where users can write  a car model, so I want to count the number of each model.
I was looking
self.env['car.model'].search_count(['car_model', '=', 'name_of_model']) So I want to count every data that user write but dinamically

Avatar
Discard

Have a look at Odoo's models.py:

@api.model

def search_count(self, args):

""" search_count(args) -> int

Returns the number of records in the current model matching :ref:`the

provided domain <reference/orm/domains>`.

"""

res = self.search(args, count=True)

return res if isinstance(res, pycompat.integer_types) else len(res)

You can use the search() method (reference can be found in the same file) with count=True. If you decorate your own method with api.depends('fields_that_trigger_recomputation') or api.onchange('...') the records will be counted every time a user creates a new one.