1

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?

David542
  • 101,766
  • 154
  • 423
  • 727

1 Answers1

1

Here is a similar question.

Catalog.objects.extra(
    select={'custom_order': 'FIELD(id, %s)' % ','.join(map(str,[1,5,3]))},
    order_by=['custom_order']
)
Community
  • 1
  • 1
Todor
  • 14,069
  • 5
  • 53
  • 61