1

Is there a way to change Paginator object_list without creating new Paginator?

I would like to order the queryset I passed to the Paginator.

Right now I'm creating new Paginator based on the ordered queryset.

Thanks

user2161049
  • 853
  • 1
  • 6
  • 13

1 Answers1

0

Ok, after all these years since this question was posted, I am getting it at the top of my google search hit list. And I have just made a simple test that proves one can change the paginator.object_list (which starts as a Django QuerySet) to a list of dictionaries, and this works seamlessly in the template. As the documentation says, the object_list can be: "A list, tuple, QuerySet, or other sliceable object with a count() or len() method."

So one can reorder the list this way, or, as I need to do, one can add more fields to the dictionary from a second database. You do need to keep the length of the replacement list to the same length as the original QuerySet, in order to use all the other features of the Paginator object. Using the QuerySet method values() allows you to retain all the fields very easily (thanks @David Wolever for this tip)

Here is some code:

new_object_list = list(records_from_paginator.object_list.values())
reordered_or_changed_object_list = reorder_or_change_object_list(new_object_list)
records_from_paginator.object_list = reordered_or_changed_object_list
excyberlabber
  • 629
  • 1
  • 10
  • 17