在我看来, 如果我导入一个 itertools 模块:
from itertools import chain
我用它链接了一些对象:
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))
然后我如何在“date_added”之前订购“ourtags”?
毫不奇怪,
ourtags = list(chain(franktags, amytags, timtags, erictags)).order_by('date_added')
返回“'list' 对象没有属性 'order_by'”错误。
最佳答案
import operator
ourtags = sorted(ourtags, key=operator.attrgetter('date_added'))
关于python - 我如何过滤 itertools chain() 结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1129344/