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
  • 104,438
  • 178
  • 489
  • 842

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
  • 15,307
  • 5
  • 55
  • 62