Skip to content

Instantly share code, notes, and snippets.

@renyi
Last active May 31, 2017 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save renyi/4522029 to your computer and use it in GitHub Desktop.
Save renyi/4522029 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