python - Sort queryset by values in list -


is possible sort django queryset list of elements provided in query? example, if

m.objects.filter(id__in=[3,1,8]) 

i wan't order of queryset element of id 3, element of id 1 , element of id 8.

thanks

no. there no way want short of contrived mechanism like:

qs = m.objects.filter(id__in=[3,1,8]) qs_sorted = list() id in [3,1,8]:     qs_sorted.append(qs.get(id=id)) 

you'd need throw exception handling in there well, in case 1 of specified ids wasn't returned.

the negatives negate lazy loading of queryset. in fact, it's generate multiple db queries (haven't tested though). also, end normal python list instead of queryset, no more filtering possible.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -