code: def get_ties_stats(self, categories, target_model=None):
          
          filter_kwargs = {
              'category_id__in': categories
          }
          if target_model is not None:
              is_cls = hasattr(target_model, '__name__')
              if is_cls:
                  concrete = False
              else:
                  concrete = True
                  filter_kwargs['object_id'] = target_model.id
              filter_kwargs['content_type'] = ContentType.objects.get_for_model(
                  target_model, for_concrete_model=concrete
              )
  
          return {
              item['category_id']: item['ties_num'] for item in
              get_tie_model().objects.filter(**filter_kwargs).values('category_id').annotate(ties_num=Count('category'))
          }
docs: Returns a dict with categories popularity stats.
  
          :param list categories:
          :param Model|None target_model:
          :return: