Django:复制和操作查询集?

发布于 2024-10-10 00:39:34 字数 616 浏览 0 评论 0原文

我需要重新排序 Django 查询集,因为我想放置 FloatField 上的 ORDER BY DESC 查询底部没有值

不幸的是,我正在努力寻找一种优雅的方法来操作 Django 查询集。这是我到目前为止所得到的:

cities = City.objects.filter(country__id=country.id).order_by('value')
if cities.count() > 1:
    cities_sorted = cities
    del manors_sorted[:]
    for city in cities:
        cities_sorted += city
        # Add code to 
        cities = cities_sorted

目前,此操作失败,并显示 'QuerySet' 对象不支持项目删除

知道如何复制并重新排序此查询集以将“无”项目放在最后吗?

I need to re-order a Django queryset, because I want to put None values at the bottom of an ORDER BY DESC query on a FloatField.

Unfortunately, I'm struggling to find an elegant way to manipulate a Django queryset. Here's what I have so far:

cities = City.objects.filter(country__id=country.id).order_by('value')
if cities.count() > 1:
    cities_sorted = cities
    del manors_sorted[:]
    for city in cities:
        cities_sorted += city
        # Add code to 
        cities = cities_sorted

Currently, this fails with 'QuerySet' object does not support item deletion.

Any idea how I can copy and re-order this QuerySet to put None items last?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

南街九尾狐 2024-10-17 00:39:34

如果您例如,查询集将被评估为一个列表。对其调用 list()

cities_sorted = list(cities)

The queryset will be evaluated to a list, if you eg. call list() on it:

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