On Wed, 2008-10-08 at 19:51 +0100, Andrew Ingram wrote: > Hi all, > > I'm using SolrPy to integrate django with solr for searching (very > simple to configure by the way, highly recommend it). But I have a > problem that I'm wonder if there's an elegant solution for either at the > Django template or Python level (I can solve the problem in a messy way, > but I like simple). > > Basically, solr gives me a list of Ids which I then use as part of an > 'in' subquery to get all the objects from the db in one query. The > problem is that SQL doesn't return the objects in the order of the > provided list (which is to be expected). But now I have a queryset of > objects which are in the wrong order.
Here's one way that comes to mind: When you do the selection based on the id's, use the in_bulk() queryset method. That returns you a dictionary mapping each id value to the object with that id (see below for how to do this without in_bulk() if that's not convenient). Once you have this dictionary, you can then convert it into an ordered list using [bulk_dict[i] for i in bulk_id_list] If using in_bulk(bulk_id_list) isn't possible, realise that it's really just a shortcut for dict([(obj.id, obj) for obj in my_queryset]) So regardless of whether you do this manually or with in_bulk(), it's two iterations over the queryset, so an O(n) operation. Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---