Can I use the Django style order_by () to sort a list of existing model objects instead of a QuerySet?

Is there an easy way to reuse the Django-style QuerySet order to sort the list of model objects.

Something that would fit:

MyModel.objects.all().order_by('-field', 'parent__field')

I just got it list(MyModel.objects.all())as an input, so I already got all the objects and just have to sort them in memory '-field', 'parent__field'.

+4
source share
1 answer

As pointed out in the comments on the OP, he changed the values ​​of the fields that will be used for ordering, and now has to reorder the list.

/ , Django ORM, , lifter, /dicts :

import lifter

my_objects = MyModel.objects.all().order_by('-field', 'parent__field')

# ... manipulate my_objects ...

ObjModel = lifter.models.Model('MyModel')
manager = ObjModel.load(my_objects)

results = manager.order_by('-field', 'parent__field')

, (, parent__field) (?), .

+2

All Articles