【发布时间】:2011-05-17 20:19:54
【问题描述】:
我的数据库设置有一个案例列表,其中包含来自我们的票务系统的 ID、位置、指定人员和从票证中提取的简短描述:
- 案例
- 身份证
- 位置
- 指定人员
- 简短说明
- 状态
现在为了帮助更好地排序,根据某些状态值,我们在查询后更改 QuerySet。
current_active = Case.objects.filter(queue_num=0,status__lt=6)
for item in current_active:
if(item.location == 'NPH'):
item.assigned_tech = 'NPH'
elif(item.location == 'Bins'):
item.assigned_tech = 'Bins'
elif(item.status > '2' and item.status < '6'):
item.assigned_tech = 'Pending'
我想知道是否有任何方法可以对这个新修改的 QuerySet 进行排序。我们知道它打印正确,只是归结为混乱的排序。
更新 2
current_active = Case.objects.filter(queue_num=0,status__lt=6)
current_active_list = []
for item in current_active:
number = item.id
item.id = '%07d' % number
phone = item.myneuname.phonenumber
if(len(phone) > 7):
formattedPhone = phone[:-10]+' ('+phone[-10:-7]+') '+phone[-7:-4]+'-'+phone[-4:]
item.myneuname.phonenumber = formattedPhone
if(item.location == 'NPH'):
item.assigned_tech = 'NPH'
elif(item.location == 'Bins'):
item.assigned_tech = 'Bins'
elif(item.status > '2' and item.status < '6'):
item.assigned_tech = 'Pending'
current_active_list.append(item)
current_active_list.sort(key=lambda x: x.assigned_tech)
虽然这不是 Django 风格的想法,但这确实有效。如果您有同样的问题并找到更合理的方法来解决此问题,请不要犹豫回复。在不保存数据的情况下,制作 Python 列表似乎是唯一可行的方法。
【问题讨论】:
标签: python django sorting django-queryset