@contextfunction
def htsort(context, objects):
 if ((not objects) or ('request' not in context)):
  return objects
 request = context['request']
 if (('sorting' not in request.GET) or (not hasattr(objects, 'order_by')) or (not hasattr(objects, 'model'))):
  return objects
 args = request.GET.getlist('sorting')
 fields = objects.model._meta.get_all_field_names()
 for arg in args:
  field_name = arg.lstrip('-')
  if (field_name in fields):
   field = objects.model._meta.get_field(field_name)
   if isinstance(field, models.ManyToManyField):
    agg_field = agg_arg = str(('sorting_%s' % field_name))
    if (arg[0] == '-'):
     agg_arg = ('-' + agg_arg)
    kwargs = {agg_field: models.Count(field_name)}
    objects = objects.annotate(**kwargs).order_by(agg_arg)
   else:
    objects = objects.order_by(arg)
 return objects.distinct()

@ contextfunction def htsort ( context , objects ) : if ( ( not objects ) or ( 'request' not in context ) ) : return objects request = context [ 'request' ] if ( ( 'sorting' not in request . GET ) or ( not hasattr ( objects , 'order_by' ) ) or ( not hasattr ( objects , 'model' ) ) ) : return objects args = request . GET . getlist ( 'sorting' ) fields = objects . model . _meta . get_all_field_names ( ) for arg in args : field_name = arg . lstrip ( '-' ) if ( field_name in fields ) : field = objects . model . _meta . get_field ( field_name ) if isinstance ( field , models . ManyToManyField ) : agg_field = agg_arg = str ( ( 'sorting_%s' % field_name ) ) if ( arg [ 0 ] == '-' ) : agg_arg = ( '-' + agg_arg ) kwargs = { agg_field : models . Count ( field_name ) } objects = objects . annotate ( ** kwargs ) . order_by ( agg_arg ) else : objects = objects . order_by ( arg ) return objects . distinct ( )
sort objects based on request .