[Answer]-Page Refresh Disrupts Sort Order. Server Restart Restores It

1👍

Your example obviously doesn’t represent your code, since you’re using strings as dictionary keys and right after says that in your code they are not strings, but query sets. Trying to answer without the real code: You’re using query sets as dictionary keys and their hash value (probably generated from their memory address) are being used to sort the final list. The query sets do not have a meaningful ordering, so after each reload you may see different results. Instead you need to sort using the query set contents.

Edit: Replace

qtylist[x.finished_case] = x.qty

With:

qtylist[x.finished_case.name] = x.qty

The former keys are finished_case objects and they don’t have a meaningful ordering. The latter are strings and have the ordering you want.

Alternative: remove both the dict and the sort. The code becomes:

qtylist = []
for x in sd:
    qtylist.append((x.finished_case.name, x.qty))
return qtylist
👤hdante

Leave a comment