Comment

Brandon Rhodes

I think the line "found = i + 1" was perhaps intended to read "count = i + 1"?

A question about style: is it common to run explicit "for" loops against Django queries? I might have expected an approach that looks at the whole result instead of building it gradually:

def get_results(queryset, fields, size):
    results = list(queryset.values(*fields)[: size + 1])
    count = len(results)
    if count > size:
        results.pop()
        count = queryset.count()
    return {"count": count, "results": results}

But that's because I personally find it easier to reason about a whole list at the same time, rather than about a loop that gradually accumulates results. Oh — but I suppose my approach founders on the fact that it creates a result object that it doesn't need, which is a waste of effort?