Skip to content

Instantly share code, notes, and snippets.

@gaozhidf gaozhidf/query.py forked from chriskief/query.py
Created Oct 19, 2018

Embed
What would you like to do?
ids = [9, 8, 1, 2, 7, 3]
results = Model.objects.filter(id__in=ids).extra(
select={'manual': 'FIELD(id,%s)' % ','.join(map(str, ids))},
order_by=['manual']
)
@gaozhidf

This comment has been minimized.

Copy link
Owner Author

gaozhidf commented Oct 19, 2018

from: https://chriskief.com/2015/01/13/sort-django-query-order-by-using-values-within-in/

SELECT id, name
FROM table
WHERE name IN (9, 8, 1, 2, 7, 3)
ORDER BY FIELD(id, 9, 8, 1, 2, 7, 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.