def query_string_ordering(context, value, **kwargs):
    """
    Add ordering param to the given query string
    :param context: template context
    :param value: examples would be '-id' or 'id'. A minus indicates that the
                  default sorting is descending
    :param kwargs: not used
    :return: Adjusted query string, starting with '?'
    """
    params = context['request'].GET.copy()

    # if the given value is '-id', it's core value would be 'id'
    core_value = value
    default_order = 'asc'

    if core_value[0] == '-':
        core_value = value[1:]
        default_order = 'desc'

    current_value = ''

    # by preference get the current ordering value from the filter
    # so that even if no explicit ordering is in the URL, we still
    # get the implicit ordering, the page's default
    # See generics.filters.FilterSet
    if 'filter' in context:
        current_value = context['filter'].ordered_value()
    elif 'ordering' in params:
        current_value = params['ordering']

    # The first two clauses check if the current ordering is on the
    # same field as the desired one, in which case we reverse the direction.
    # If it's on another field, we use the default direction.
    if current_value == core_value:
        order_prefix = '-'
    elif current_value == '-' + core_value:
        order_prefix = ''
    elif default_order == 'desc':
        order_prefix = '-'
    else:
        order_prefix = ''

    params['ordering'] = order_prefix + core_value
    return '?' + params.urlencode()