def fallback_generic_annotate(qs_model, generic_qs_model, aggregator, gfk_field=None, alias='score'):
    qs = normalize_qs_model(qs_model)
    generic_qs = normalize_qs_model(generic_qs_model)
    
    content_type = ContentType.objects.get_for_model(qs.model)
    
    qn = connection.ops.quote_name

    if django.VERSION < (1, 8):
        aggregate_field = aggregator.lookup
    else:
        aggregate_field = aggregator.default_alias.rsplit('__', 1)[0]

    # since the aggregate may contain a generic relation, strip it
    if '__' in aggregate_field:
        _, aggregate_field = aggregate_field.rsplit('__', 1)
    
    if gfk_field is None:
        gfk_field = get_gfk_field(generic_qs.model)
    
    # collect the params we'll be using
    params = (
        aggregator.name, # the function that's doing the aggregation
        qn(aggregate_field), # the field containing the value to aggregate
        qn(gfk_field.model._meta.db_table), # table holding gfk'd item info
        qn(gfk_field.ct_field + '_id'), # the content_type field on the GFK
        content_type.pk, # the content_type id we need to match
        gfk_expression(qs.model, gfk_field),
        qn(qs.model._meta.db_table), # the table and pk from the main
        qn(qs.model._meta.pk.name)   # part of the query
    )
    
    sql_template = """
        SELECT %s(%s) AS aggregate_score
        FROM %s
        WHERE
            %s=%s AND
            %s=%s.%s"""
    
    extra = sql_template % params
    
    if generic_qs.query.where.children:
        generic_query = generic_qs.values_list('pk').query
        inner_query, inner_query_params = query_as_sql(generic_query)
        
        inner_params = (
            qn(generic_qs.model._meta.db_table),
            qn(generic_qs.model._meta.pk.name),
        )
        inner_start = inner_params % ' AND %s.%s IN ('
        inner_end = ')'
        extra = extra + inner_start + inner_query + inner_end
    else:
        inner_query_params = []

    return qs.extra(
        select={alias: extra},
        select_params=inner_query_params,
    )
dataset/ETHPy150Open coleifer/django-generic-aggregation/generic_aggregation/utils.py/fallback_generic_annotate/swapped operands of `%`