0

Sphinx returns results as a dictionary. For example, the sequence of resulted IDs is [3,1,5,2,4] ordered by sphinx field weights. When passing them to a table object as a QuerySet I get table with objects sorted by IDs like [1,2,3,4,5]. How to preserve the right order of items in a queryset?

result = c.Query(q, 'db')
ids = [obj['id'] for obj in result['matches']]
queryset = Model.objects.filter(id__in=ids)
table = Table(queryset)

I can get rid of using tables at all, but it takes too long to execute something like

objects = [Model.objects.get(pk=obj['id']) for obj in result['matches']]

How it can be optimised?

Dmitry
  • 3
  • 5
  • 1
    I think this is hard to do with the django ORM. see this SO question/answer: http://stackoverflow.com/questions/4916851/django-get-a-queryset-from-array-of-ids-in-specific-order – Sohan Jain Apr 03 '14 at 00:47
  • Thanks Sohan. It solves my problem. – Dmitry Apr 03 '14 at 12:20

0 Answers0