sort_by_id_sequence

 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

  1. Pagination/Filtering Alphabetically by zain 4 years, 2 months ago
  2. Chain multiple querysets into one by mattdw 4 years, 7 months ago
  3. Counter model - run multiple persistent counters by simon 3 years, 11 months ago
  4. Django model objects and querysets dehydration/hydration by Kronuz 1 year, 6 months ago
  5. Prefill ForeignKey caches by insin 4 years, 7 months ago

Comments

(Forgotten your password?)