David542
David542

Reputation: 110382

ORDER BY id array in django ORM

How would I do the following in the django ORM:

select * from catalog order by field(id, list_of_ids)

So far I have:

ids = [1,5,3]
Catalog.objects.extra(order_by=[...?])

What would be the correct way to do this?

Upvotes: 1

Views: 174

Answers (1)

Todor
Todor

Reputation: 16050

Here is a similar question.

Catalog.objects.extra(
    select={'custom_order': 'FIELD(id, %s)' % ','.join(map(str,[1,5,3]))},
    order_by=['custom_order']
)

Upvotes: 1

Related Questions