假设我们有一个基本模型。
class Log(models.Model):
key = fields.BigInteger()
calldate = fields.DateTimeField()
followupdate = fields.DateTimeField()
可以为同一个键设置多个 followupdates。我想做的是在列表中显示最近(按通话日期)安排的后续行动。
在我看来,我有以下几点:
# 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
通过 key重新排序字典 , followupdate
。在每个字典中都有一个这样的键:'followupdate': datetime.date(2013, 3, 25)
这是我需要重新排序的。