diox said:

since you only care about the position, to make the query less horrible I suggest something like this:

    adding a qs = qs.values_list('user_id', flat=True)
    using d.get('user_id')

(My idea is to only fetch the user_ids, no other fields, and not building useless UserProfile instances)

---

I renamed the `qs` to `values_qs` as it's actually a `ValuesQuerySet` object. It is converted to a `list` so it's obvious it does not read from queryset object inside the loop.