pythonodooodoo-10odoo-11odoo-12

Odoo: search_count do not show archived record


I have a button called job_count which links jobs to equipment. It is only showing active records of job, But, I want to show the count of all the active as well as an archived record of jobs associated with equipment. Please help.

  def _get_job_count(self):
      for record in self:
          j_count = self.env['job_module'].sudo().search_count([('equipment_id', 'in', self.ids)]])
          record.job_count= j_count

Solution

  • active
    toggles the global visibility of the record, if active is set to False the record is invisible in most searches and listing.

    You can manually tell the search method to find also archived records by adding the following criteria to the search domain:

     '|', ('active','=',True),  ('active','=',False)
    

    Example:

    search_count([('equipment_id', 'in', self.ids), '|', ('active','=',True),  ('active','=',False)])
    

    Shortcut:

    Odoo provide a shortcut to set the active flag, you just need to specify active_test in the context.

    whether the default filtering of records with active
    field set to False should be applied.

    In old API, the active_test is passed through the context parameter:

    .search_count(cr, uid, domain, context=dict(context, active_test=False))
    

    In new API, you can use with_context method to update the search context:

    self.with_context(active_test=False).search_count(domain)