Python / Django — Combine Querysets and Sort. Sort List of Any Objects.

Assuming it quacks like a duck, you can use operator.attrgetter()

Combine queryset by turning it into a list:
queryset_list = list(queryset1).extend(list(queryset2))

Sort the list by:
queryset_list.sort(key=operator.attrgetter(‘attribute to sort by’))

Update: 4/28/2010

Yeah, I forgot the sort function sorts in place. sort = list.sort would not be good.
Also, for those wondering, you can just import operator. “import operator”

Advertisements

2 thoughts on “Python / Django — Combine Querysets and Sort. Sort List of Any Objects.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s