Is there a way to make a request regarding the order of the entered parameters?

(Please let me know if this is completely absurd, maybe this is the reason I didn't find anything on this.)

This story has two models Rankingand Artist, Rankingin general, is associated with Artist(object_id, content_type ... the whole shebang).

I have a list of objects returned Ranking.objects.values_list()ordered by a specific field (in my case score). Therefore, obviously, if I want to display a list of artists who have been ranked, I would like them to be in the same order. I tried different methods such as .filter(pk__in=list), .in_bulk(list)etc. Tried forced execution of the result .values_list()in the tuple too.

They all take my list:

>>> objects = Ranking.objects.filter(<stuff>).order_by('score')
>>> objects_list = objects.values_list('object_id', flat=True)
>>> objects_list
[8, 1, 2, 15, 14, 3, 13, 31, 16, 5, 4, 7, 32, 9, 37]

And return it like this:

>>> Artist.objects.filter(id__in=objects_list).values_list('id', flat=True)
[7, 32, 3, 8, 4, 2, 31, 9, 37, 13, 16, 1, 5, 15, 14]

( .)

- values_list().

for item in objects:
    ranked_items.append(item.content_object)

n, , . , PostgreSQL.

+3
1

Django; , .

+2

All Articles