python - Yet another QuerySet custom ordering problem -


lots of searching, found nothing far.

in django app, model_x rated users using standard ratings model. have standalone function processes lot of data , when given user instance, returns list of tuples. first value corresponds primary key instance of model_x, , second predicted rating. example, might return following list of tuples.

[(1l, 5.25), (5l, 3.1), (23l, 1.83)] 

i need return queryset (and needs queryset) of model_x, containing instances listed in tuples, sorted these predicted ratings tuples.

if list of rating tuples ordered might able use ordering order of values in sql in() clause.

if you're using mysql field() function should work you. pull ids sorted rating tuples, [x[0] x in sorted_tuples] , use list of ids build clause, e.g. (untested),

model_x.objects.raw('select * myapp_model_x id in (5,1,23) order field(id,5,1,23)'])  

Comments