Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save philippeowagner/9263b6460a61e8c47cedce9e1d3ba3d3 to your computer and use it in GitHub Desktop.
Save philippeowagner/9263b6460a61e8c47cedce9e1d3ba3d3 to your computer and use it in GitHub Desktop.
Queryset sorting
# http://stackoverflow.com/questions/431628/how-to-combine-2-or-more-querysets-in-a-django-view
from itertools import chain
from operator import attrgetter
def join_list(*args, sort_key=None):
if sort_key:
return sorted(chain(args), key=attrgetter(sort_key))
return list(chain(args))
# Unsorted
teacher_list = assistant_teacher_list | subjectgroup_teacher_list | other_teacher_list
#Sorted
teacher_list = join_list(assistant_teacher_list, subjectgroup_teacher_list, other_teacher_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment