1 2 3 4 5 6 7 8 9 10 | def sort_by_id_sequence(objects, id_sequence):
max_score = len(objects)
def sort_score_for_object(obj):
# A lower score is better
id = obj.id
try:
return id_sequence.index(id)
except ValueError: # Not found
return max_score
return sorted(objects, key=sort_score_for_object)
|
More like this
- Pagination/Filtering Alphabetically by zain 4 years, 2 months ago
- Chain multiple querysets into one by mattdw 4 years, 7 months ago
- Counter model - run multiple persistent counters by simon 3 years, 11 months ago
- Django model objects and querysets dehydration/hydration by Kronuz 1 year, 6 months ago
- Prefill ForeignKey caches by insin 4 years, 7 months ago
Comments