nodes: 0, module; 1, function_definition; 2, function_name:iterator; 3, parameters; 4, block; 5, identifier:self; 6, expression_statement; 7, expression_statement; 8, if_statement; 9, return_statement; 10, comment:"""
          If search has occurred and no ordering has occurred, decorate
          each result with the number of search terms so that it can be
          sorted by the number of occurrence of terms.
  
          In the case of search fields that span model relationships, we
          cannot accurately match occurrences without some very
          complicated traversal code, which we won't attempt. So in this
          case, namely when there are no matches for a result (count=0),
          and search fields contain relationships (double underscores),
          we assume one match for one of the fields, and use the average
          weight of all search fields with relationships.
          """; 11, assignment; 12, boolean_operator; 13, block; 14, identifier:results; 15, identifier:results; 16, call; 17, attribute; 18, not_operator; 19, expression_statement; 20, for_statement; 21, return_statement; 22, attribute; 23, argument_list; 24, identifier:self; 25, identifier:_search_terms; 26, attribute; 27, assignment; 28, pattern_list; 29, call; 30, block; 31, call; 32, call; 33, identifier:iterator; 34, identifier:self; 35, identifier:_search_ordered; 36, identifier:results; 37, call; 38, identifier:i; 39, identifier:result; 40, identifier:enumerate; 41, argument_list; 42, expression_statement; 43, expression_statement; 44, for_statement; 45, if_statement; 46, expression_statement; 47, identifier:iter; 48, argument_list; 49, identifier:super; 50, argument_list; 51, identifier:list; 52, argument_list; 53, identifier:results; 54, assignment; 55, assignment; 56, tuple_pattern; 57, call; 58, block; 59, boolean_operator; 60, block; 61, assignment; 62, identifier:results; 63, identifier:SearchableQuerySet; 64, identifier:self; 65, identifier:results; 66, identifier:count; 67, integer:0; 68, identifier:related_weights; 69, list; 70, identifier:field; 71, identifier:weight; 72, attribute; 73, argument_list; 74, if_statement; 75, for_statement; 76, not_operator; 77, identifier:related_weights; 78, expression_statement; 79, attribute; 80, identifier:count; 81, attribute; 82, identifier:items; 83, comparison_operator:"__" in field; 84, block; 85, identifier:term; 86, attribute; 87, block; 88, identifier:count; 89, assignment; 90, subscript; 91, identifier:result_count; 92, identifier:self; 93, identifier:_search_fields; 94, string:"__"; 95, identifier:field; 96, expression_statement; 97, identifier:self; 98, identifier:_search_terms; 99, expression_statement; 100, if_statement; 101, identifier:count; 102, call; 103, identifier:results; 104, identifier:i; 105, call; 106, assignment; 107, identifier:field_value; 108, block; 109, identifier:int; 110, argument_list; 111, attribute; 112, argument_list; 113, identifier:field_value; 114, call; 115, expression_statement; 116, binary_operator:sum(related_weights) / len(related_weights); 117, identifier:related_weights; 118, identifier:append; 119, identifier:weight; 120, identifier:getattr; 121, argument_list; 122, augmented_assignment; 123, call; 124, call; 125, identifier:result; 126, identifier:field; 127, None; 128, identifier:count; 129, binary_operator:field_value.lower().count(term) * weight; 130, identifier:sum; 131, argument_list; 132, identifier:len; 133, argument_list; 134, call; 135, identifier:weight; 136, identifier:related_weights; 137, identifier:related_weights; 138, attribute; 139, argument_list; 140, call; 141, identifier:count; 142, identifier:term; 143, attribute; 144, argument_list; 145, identifier:field_value; 146, identifier:lower
code: def iterator(self):
          """
          If search has occurred and no ordering has occurred, decorate
          each result with the number of search terms so that it can be
          sorted by the number of occurrence of terms.
  
          In the case of search fields that span model relationships, we
          cannot accurately match occurrences without some very
          complicated traversal code, which we won't attempt. So in this
          case, namely when there are no matches for a result (count=0),
          and search fields contain relationships (double underscores),
          we assume one match for one of the fields, and use the average
          weight of all search fields with relationships.
          """
          results = super(SearchableQuerySet, self).iterator()
          if self._search_terms and not self._search_ordered:
              results = list(results)
              for i, result in enumerate(results):
                  count = 0
                  related_weights = []
                  for (field, weight) in self._search_fields.items():
                      if "__" in field:
                          related_weights.append(weight)
                      for term in self._search_terms:
                          field_value = getattr(result, field, None)
                          if field_value:
                              count += field_value.lower().count(term) * weight
                  if not count and related_weights:
                      count = int(sum(related_weights) / len(related_weights))
                  results[i].result_count = count
              return iter(results)
          return results