views:

382

answers:

2

in my views, if i import an itertools module:

from itertools import chain

and i chain some objects with it:

franktags = Frank.objects.order_by('date_added').reverse().filter(topic__exact='art') 
amytags = Amy.objects.order_by('date_added').reverse().filter(topic__exact='art') 
timtags = Tim.objects.order_by('date_added').reverse().filter(topic__exact='art') 
erictags = Eric.objects.order_by('date_added').reverse().filter(topic__exact='art')

ourtags = list(chain(franktags, amytags, timtags, erictags))

how do i then order "ourtags" by the "date_added"?

not surpisingly,

ourtags = list(chain(franktags, amytags, timtags, erictags)).order_by('date_added')

returns an "'list' object has no attribute 'order_by'" error.

+2  A: 

By this point in the code, you've already loaded up all of the objects into memory and into a list. Just sort the list like you would any old Python list.

>>> import operator
>>> ourtags.sort(key=operator.attrgetter('date_added'))
FogleBird
thanks a million for your answer FB. very helpful!
+2  A: 
import operator

ourtags = sorted(ourtags, key=operator.attrgetter('date_added'))
Alex Martelli
i thought the answer would be straightforward. thank goodness i was right. thanks so much for your help.