【问题标题】:Django: copy and manipulate a QuerySet?Django:复制和操作查询集?
【发布时间】:2010-12-31 11:22:13
【问题描述】:

我需要重新排序一个 Django 查询集,因为我想输入None values at the bottom of an ORDER BY DESC query on a FloatField

不幸的是,我正在努力寻找一种优雅的方式来操作 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' object does not support item deletion 失败。

知道如何复制和重新排序此 QuerySet 以将 None 项放在最后吗?

【问题讨论】:

    标签: django django-models django-views


    【解决方案1】:

    查询集将被评估为一个列表,例如。拨打list()就可以了:

    cities_sorted = list(cities)
    

    【讨论】:

      猜你喜欢
      • 2012-10-14
      • 2017-02-22
      • 2016-12-28
      • 1970-01-01
      • 2022-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多