-
-
Save philippeowagner/9263b6460a61e8c47cedce9e1d3ba3d3 to your computer and use it in GitHub Desktop.
Queryset sorting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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