AP257
AP257

Reputation: 93813

Django: copy and manipulate a 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?

Upvotes: 1

Views: 2865

Answers (1)

Bernhard Vallant
Bernhard Vallant

Reputation: 50786

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

cities_sorted = list(cities)

Upvotes: 2

Related Questions