def iterator(self):
    "\n        If search has occurred and no ordering has occurred, decorate\n        each result with the number of search terms so that it can be\n        sorted by the number of occurrence of terms.\n\n        In the case of search fields that span model relationships, we\n        cannot accurately match occurrences without some very\n        complicated traversal code, which we won't attempt. So in this\n        case, namely when there are no matches for a result (count=0),\n        and search fields contain relationships (double underscores),\n        we assume one match for one of the fields, and use the average\n        weight of all search fields with relationships.\n        "
    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
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.
def iterator(self):
    "\n        If search has occurred and no ordering has occurred, decorate\n        each result with the number of search terms so that it can be\n        sorted by the number of occurrence of terms.\n\n        In the case of search fields that span model relationships, we\n        cannot accurately match occurrences without some very\n        complicated traversal code, which we won't attempt. So in this\n        case, namely when there are no matches for a result (count=0),\n        and search fields contain relationships (double underscores),\n        we assume one match for one of the fields, and use the average\n        weight of all search fields with relationships.\n        "
    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