0

Lots of searching, found nothing so far.

In my django app, Model_X is rated by my users using a very standard ratings model. I have a standalone function that processes a LOT of data and when given a user instance, returns a list of tuples. The first value corresponds to the primary key for an instance of Model_X, and the second is the predicted rating. For example, it might return the following list of tuples.

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

I need to return a QuerySet (and it needs to be a QuerySet) of Model_X, containing only the instances listed in the tuples, sorted by these predicted ratings from the tuples.

Piper Merriam
  • 2,774
  • 2
  • 24
  • 30
  • I know that I can do this using the python sorted() function but I need this to return a QuerySet which to my understanding cannot be done with the sorted() function. – Piper Merriam Apr 29 '11 at 04:05
  • Can you add some code which explains you models. I don't get exactly what your example is representing. Field values from one model and its instances or from different models combined instances? – Torsten Engelbrecht Apr 29 '11 at 04:12
  • I've reworded my question with hopes of making it a bit clearer. Does this help? The field values on the model don't come into play at all in this process. – Piper Merriam Apr 29 '11 at 06:28
  • 1
    do you really need to return a QuerySet? – vad Apr 29 '11 at 06:41
  • I have a O(n) solution in mind, where you are are chaining lookups using the values in the list to maintain order... But hopefully someone comes by with a better solution. – dting Apr 29 '11 at 10:01
  • I'll take the O(n) solution over the fat NOTHING I have right now. – Piper Merriam Apr 29 '11 at 12:12
  • @vad My app has a very intensive view that renders 4 different models, sorts, filters, searches, paginates, etc and it is built on the back of being able to use the order_by method. I have already begun brainstorming how this will need to be changed if I can't get a solution to this problem, but for now I'm holding onto hope. – Piper Merriam Apr 29 '11 at 12:13
  • @Aaron: but if you sort using reputation, then you can't order_by. Better: you can, but it's not so good ;) – vad Apr 29 '11 at 12:33

2 Answers2

1

If your list of rating tuples is already ordered you might be able to use something from Ordering by the order of values in a SQL IN() clause.

If you're using MySQL then the field() function should work for you. Pull all the ids from your sorted rating tuples, [x[0] for x in sorted_tuples] and then use that list of ids to build your where clause, e.g. (untested),

Model_X.objects.raw('select * from myapp_model_x where id in (5,1,23) order by field(id,5,1,23)']) 
Community
  • 1
  • 1
A Lee
  • 7,828
  • 4
  • 35
  • 49
  • I may be doing this wrong, but using your syntax it spits out an error starting at the 'order by field...' statement inside the where statement. – Piper Merriam Apr 29 '11 at 20:17
  • ah, that's unfortunate. You may have to drop down to a [raw query](http://docs.djangoproject.com/en/dev/topics/db/sql/) to run that properly. Are you using MySQL? – A Lee Apr 29 '11 at 20:38
  • I'm going to just resort to my alternative which is to use python's sorted and rewrite my view. I can't believe that this can't be done. Seems like a major shortfall that needs to be addressed. – Piper Merriam May 02 '11 at 14:49
0

Use a list instead of a QuerySet. You can easily build it using in_bulk

vad
  • 1,196
  • 9
  • 22