我有一个带有日期列的模型Photo
。我正在制作一个照片库,有一个概览视图,显示了每年最多5张照片,有照片。你知道吗
目前,我有这个查询,并让它循环通过手工制作的年度列表。
(即for year in range(2000, 2016):
)
Photo.query \
.filter(extract('year', Photo.date)==year) \
.order_by(Photo.date) \
.limit(5) \
.all()
有没有更有效的方法(而不是15个查询)?还有,但不太重要的是,有没有一种方法可以根据照片存在的年份对年份进行排序(作为使用硬编码列表的替代方法)?你知道吗
更新:我正在使用sqlite3
假设
date
是唯一的,下面的查询应该适用于sqlite
:如果
date
不是唯一的,但是几乎是唯一的,则结果不一定总是5行,但可以是多行或少行。如果这些真的是date
值(没有时间成分),让我知道-应该很容易得到一个更健壮的解决方案。你知道吗相关问题 更多 >
编程相关推荐