Odoo: search_count do not show archived record

Question:

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
Asked By: Bishal Ghimire

||

Answers:

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)
Answered By: Kenly

You just have to add context active_test=False.

 def _get_job_count(self):
      for record in self:
          j_count = self.env['job_module'].sudo().with_context(active_test=False).search_count([('equipment_id', 'in', self.ids)]])
          record.job_count= j_count
Answered By: Purvi Bhatt

I solved it by getting a count of both active and archive records individually and then by adding them together.

  def _get_job_count(self):
      for record in self:
          j_count1 = self.env['job_module'].sudo().search_count([('active','=',False), 
            ('equipment_id', 'in', self.ids)]])
          j_count2 = self.env['job_module'].sudo().search_count([('active','=',True), 
            ('equipment_id', 'in', self.ids)]])
          record.job_count= j_count1 + j_count2 
Answered By: Bishal Ghimire