重新排序修改后的查询集
我的数据库设置了一个案例列表,其中包含来自我们的票务系统的 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'
我想知道是否有任何方法可以对这个新修改的查询集进行排序。我们知道它打印正确,只是排序混乱了。
更新 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 列表似乎是不保存数据的唯一可能的方法。
My database is set up with a list of cases with an ID from our ticketing system, a location, an assigned person and a short description taken from the ticket:
- Case
- ID
- Location
- Assigned Person
- Short Description
- Status
Now to help better sort things, depending on certain status values, we change the QuerySet after querying it.
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'
I was wondering if there was any way to sort this new modified QuerySet. We know it prints correctly, it just comes down to the sorting that gets messed up.
UPDATE 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)
While this is not idea and Django-style, this does work. If you have the same issue and find a more reasonable way to fix this problem, please don't hesitate to reply. Making a Python list seemed the only way possible without saving data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于您正在评估查询集,然后修改某些元素而不保存,因此您需要避免返回数据库并重新评估它的任何内容。就您而言,简单地在 Python 中对列表进行排序并没有什么问题 - 例如:
按
signed_tech
字段排序。Since you're evaluating the queryset and then modifying some of the elements without saving, you need to avoid anything that goes back to the database and re-evaluates it. In your case, there's nothing wrong with simply ordering the list in Python - for example:
to sort by the
assigned_tech
field.也许您正在谈论 django 的 order_by 方法ORM
Maybe you're talking about order_by method of django's ORM
不确定如何根据新属性对相同的 QuerySet 进行排序,但如果您要将每个
item
附加到新列表中(在将signed_tech
分配给每个项目之后),你可以这样做:并反转排序顺序:
但如果你找到一种方法来做到这一点而不创建新列表,那显然会更好,我很乐意看到它。
Not sure how you would sort that same QuerySet based on your new property, but if you were to append each
item
into a new list (after assigningassigned_tech
to each item), you could do something like this:And to reverse the sort order:
But if you find a way to do it without creating a new list, that would obviously be better and I would love to see it.