重新排序修改后的查询集

发布于 2024-11-07 16:46:32 字数 1474 浏览 2 评论 0原文

我的数据库设置了一个案例列表,其中包含来自我们的票务系统的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

椒妓 2024-11-14 16:46:32

由于您正在评估查询集,然后修改某些元素而不保存,因此您需要避免返回数据库并重新评估它的任何内容。就您而言,简单地在 Python 中对列表进行排序并没有什么问题 - 例如:

current_active.sort(key=lambda x: x.assigned_tech)

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:

current_active.sort(key=lambda x: x.assigned_tech)

to sort by the assigned_tech field.

无人接听 2024-11-14 16:46:32

也许您正在谈论 django 的 order_by 方法ORM

Maybe you're talking about order_by method of django's ORM

不回头走下去 2024-11-14 16:46:32

不确定如何根据新属性对相同的 QuerySet 进行排序,但如果您要将每个 item 附加到新列表中(在将 signed_tech 分配给每个项目之后),你可以这样做:

import operator
new_list.sort(key=operator.attrgetter('assigned_tech'))

并反转排序顺序:

import operator
new_list.sort(key=operator.attrgetter('assigned_tech'), reverse=True)

但如果你找到一种方法来做到这一点而不创建新列表,那显然会更好,我很乐意看到它。

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 assigning assigned_tech to each item), you could do something like this:

import operator
new_list.sort(key=operator.attrgetter('assigned_tech'))

And to reverse the sort order:

import operator
new_list.sort(key=operator.attrgetter('assigned_tech'), reverse=True)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文