On Fri, Apr 10, 2009 at 7:40 PM, Malcolm Tredinnick < malc...@pointy-stick.com> wrote:
> > On Fri, 2009-04-10 at 16:44 -0400, Alex Gaynor wrote: > > > > > > On Fri, Apr 10, 2009 at 4:40 PM, veearrsix <stup...@googlemail.com> > > wrote: > > > > This question seems to have been asked a few times, but never > > answered > > fully. > > Not quite true. It's been answered fully a lot of times: querysets are > iterators, so you have multiple sorted iterators and combining them into > a single sorted iterator is just a merge sort. > > Creating a single queryset is almost always not the real problem people > are trying to solve. Which is fortunate, since it doesn't make sense on > a definitional level: a queryset is the result of filtering a set of > model results, not combining arbitrary SQL. Combining multiple querysets > into a single result that can be iterated through is the typical problem > trying to be solved. > > [...] > > > > There is no way to get a single QuerySet with results from multiple > > models. What you want to do is best approximated like this: > > > > from itertools import chain > > > > sorted(chain(Model1.obejcts.all(), Model2.objects.all()), key=lambda > > o: o.pub_date) > > That's a bit crufty, since it pulls everything into memory immediately. > Not cool with a few tens of thousands items in the result set (creating > all those Python objects is noticeable). > > Merge or heap sort is your friend here. Sure, us old guys have the > advantage here, because that stuff was very standard back in the age of > dinosaurs when RAM was scarcer and hard- and tape-drives were slower, so > random access wasn't always an option. > > Particularly with iterators, storing the (next head item, rest of > iterator) pair in a heap leads to a very neat and fast way to create a > combined iterator. Here's one professional-quality implementation that I > would recommend: http://code.activestate.com/recipes/491285/ > It has one unfortunate failing(which is the fault of the heapq library), it doesn't take a key/cmp function like sorted does. It is otherwise completely excellent though. > > Regards, > Malcolm > > > > > Alex -- "I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire "The people's good is the highest law."--Cicero --~--~---------~--~----~------------~-------~--~----~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---