假设我们有一个基本模型。
class Log(models.Model):
key = fields.BigInteger()
calldate = fields.DateTimeField()
followupdate = fields.DateTimeField()
可以为同一个键设置多个后续更新。我想要做的是在列表中出现最近(通过calldate)预定的后续行动。
在我看来,我有以下内容:
# views.py
def callbacks(request):
""" get objects where a followupdate has been specified """
q = Log.objects.filter(followupdate__isnull = False).order_by("-calldate")
""" deduplicate key so only most recently scheduled followupdate show """
newresults = []
seen_key = []
for result in q:
if result.key not in seen_key:
seen_key.append(result.key)
newresults.append(result)
results = newresults
""" What I want to do is equivalent to results.order_by("followupdate") """
""" But since it's a dictionary now and not a queryset, I can't """
return render_to_response('callbacks.html', {"callbacks":results})
我需要帮助的是通过密钥results
重新排序字典followupdate
。在每个字典中有一个这样的键:'followupdate': datetime.date(2013, 3, 25)
这是我需要重新排序的。
答案 0 :(得分:2)
.sort()
似乎就是这样 -
l.sort(key=lambda item:item['followupdate'], reverse=True)
测试用例 -
>>> l = [{'followupdate': datetime.date(2013, 3, 25)}, {'followupdate': datetime.date(2013, 3, 24)}, {'followupdate': datetime.date(2013, 3, 29)}]
>>> l
[{'followupdate': datetime.date(2013, 3, 25)}, {'followupdate': datetime.date(2013, 3, 24)}, {'followupdate': datetime.date(2013, 3, 29)}]
>>> l.sort(key=lambda item:item['followupdate'], reverse=True)
>>> l
[{'followupdate': datetime.date(2013, 3, 29)}, {'followupdate': datetime.date(2013, 3, 25)}, {'followupdate': datetime.date(2013, 3, 24)}]
答案 1 :(得分:2)
你应该让数据库进行重复数据删除,而不是从数据库中提取记录只是为了在Python中丢弃它们,这是浪费时间和带宽。
Django的对象关系映射不是这个,但你可以在SQL中这样做:
SELECT L1.*
FROM myapp_log AS L1,
(SELECT MAX(date) AS maxdate, `key`
FROM myapp_log GROUP BY `key`) as L2
WHERE L1.date = L2.maxdate
AND L1.`key` = L2.`key`
AND L1.followupdate IS NOT NULL
ORDER BY L1.date DESCENDING
(字段名称key
需要引用,因为它是一个SQL关键字。)
然后,您可以使用raw()
method从Django运行此查询,如下所示:
sql = '''
SELECT L1.* FROM myapp_log AS L1,
# and so on (see above)
'''
q = Log.objects.raw(sql)