Django:复制和操作QuerySet?

时间:2010-12-31 11:22:13

标签: django django-models django-views

我需要重新订购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以将无项目放在最后?

1 个答案:

答案 0 :(得分:2)

如果你是例如,查询集将被评估为一个列表。在其上拨打list()

cities_sorted = list(cities)