python - 我如何过滤 itertools chain() 结果?

标签 python django django-views python-itertools

在我看来, 如果我导入一个 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/

相关文章:

python - 从 URL、RE、python 中提取 Amzon ASIN

mysql - 使用 Django 将现有的 MyISAM 数据库转换为 InnoDB

python - 如何使用 pymysql 在 python 中选择没有列名的选项?

python - 导入错误 : Keras requires TensorFlow 2. 2 或更高。通过 `pip install tensorflow` 安装 TensorFlow

python - 转到 "http://127.0.0.1:8000/restaurant/sign-in/"页面时出现页面未找到 (404) 错误

具有多对多关系的Django表单不保存

python - 类型错误 : Object of type 'int32' is not JSON serializable

django 从相关模型(外键)获取值

Python:检查变量是否是字典

python - Django - 模板未加载,永久 302 消息