(db_name, model_class, q_expression, annotate=None, query_size=5000, values=None,
                   load_source=None)
Runs a query on the given database in small chunks and produces a generator
    with the results.

    Iteration logic adopted from https://djangosnippets.org/snippets/1949/

    :param model_class: A Django model class

    :param q_expression: An instance of django.db.models.Q representing the
    filter to apply

    :param annotate: (optional) If specified, should be a dictionary of annotated fields
    and their calculations. The dictionary will be splatted into the `.annotate` function

    :param values: (optional) If specified, should be a list of values to retrieve rather
    than retrieving entire objects.

    :return: A generator with the results
Runs a query on the given database in small chunks and produces a generator
    with the results.
Runs a query on the given database in small chunks and produces a generator with the results .
def paginate_query(db_name, model_class, q_expression, annotate=None, query_size=5000, values=None,
                   load_source=None):
    """
    Runs a query on the given database in small chunks and produces a generator
    with the results.

    Iteration logic adopted from https://djangosnippets.org/snippets/1949/

    :param model_class: A Django model class

    :param q_expression: An instance of django.db.models.Q representing the
    filter to apply

    :param annotate: (optional) If specified, should be a dictionary of annotated fields
    and their calculations. The dictionary will be splatted into the `.annotate` function

    :param values: (optional) If specified, should be a list of values to retrieve rather
    than retrieving entire objects.

    :return: A generator with the results
    """

    track_load = load_counter_for_model(model_class)(load_source, None)
    sort_col = 'pk'

    return_values = None
    if values:
        return_values = [sort_col] + values

    qs = model_class.objects.using(db_name)
    if annotate:
        qs = qs.annotate(**annotate)

    qs = qs.filter(q_expression).order_by(sort_col)

    if return_values:
        qs = qs.values_list(*return_values)

    filter_expression = {}
    while True:
        results = qs.filter(**filter_expression)[:query_size]
        for row in results:
            track_load()
            if return_values:
                value = row[0]
                yield row[1:]
            else:
                value = row.pk
                yield row

        if len(results) < query_size:
            break

        filter_expression = {'{}__gt'.format(sort_col): value}
def paginate_query ( db_name , model_class , q_expression , annotate = None , query_size = 5000 , values = None , load_source = None ) : track_load = load_counter_for_model ( model_class ) ( load_source , None ) sort_col = 'pk' return_values = None if values : return_values = [ sort_col ] + values qs = model_class . objects . using ( db_name ) if annotate : qs = qs . annotate ( * * annotate ) qs = qs . filter ( q_expression ) . order_by ( sort_col ) if return_values : qs = qs . values_list ( * return_values ) filter_expression = { } while True : results = qs . filter ( * * filter_expression ) [ : query_size ] for row in results : track_load ( ) if return_values : value = row [ 0 ] yield row [ 1 : ] else : value = row . pk yield row if len ( results ) < query_size : break filter_expression = { '{}__gt' . format ( sort_col ) : value }