Skip to content

Instantly share code, notes, and snippets.

@Ell
Created September 13, 2011 19:52
Show Gist options
  • Save Ell/1214908 to your computer and use it in GitHub Desktop.
Save Ell/1214908 to your computer and use it in GitHub Desktop.
def apply_sorting(self, obj_list, options=None):
""" override apply_sorting to enable random sorting """
try:
return super(CatResource, self).apply_sorting(obj_list, options)
except InvalidSortError:
parameter_name = 'order_by'
if hasattr(options, 'getlist'):
order_bits = options.getlist(parameter_name)
else:
order_bits = options.get(parameter_name)
if not isinstance(order_bits, (list, tuple)):
order_bits = [order_bits]
for order_by in order_bits:
order_by_bits = order_by.split(LOOKUP_SEP)
field_name = order_by_bits[0]
if field_name == 'random':
return obj_list.order_by('?')
elif field_name == '?':
return obj_list.order_by('?')
else:
raise InvalidSortError("The '%s' field does not allow ordering." % field_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment