After reading this thread and answers on SO (e.g. How to combine 2 or more 
querysets in a Django view? 
<https://stackoverflow.com/questions/431628/how-to-combine-2-or-more-querysets-in-a-django-view?rq=1>)
 
I'm still not sure if this code is fully lazy with Django 1.11+ or if it is 
going to load everything into memory:

qs1 = Model_A.objects.filter(foofield='foo').order_by('created_at')
qs2 = Model_B.objects.filter(barfield='bar').order_by('created_at')

paginator = Paginator(sorted(chain(qs1, qs2),
                             key=lambda i:i.created_at
                            ),
                      50)


If the querysets each referred to 500k results, would this blow up?








On Wednesday, April 15, 2009 at 8:49:08 AM UTC-4, veearrsix wrote:
>
> Thanks guys that was perfect 
>
> On Apr 14, 3:53 pm, Alex Gaynor <alex.gay...@gmail.com> wrote: 
> > On Tue, Apr 14, 2009 at 10:50 AM, veearrsix <stup...@googlemail.com> 
> wrote: 
> > 
> > > Thanks for the help so far guys, I've used that recipe suggested by 
> > > Malcolm, where by I pass a number of querysets into the method. It 
> > > works as expected I guess, BUT, I would like to be able to sort by the 
> > > datetime field from each queryset, however the fieldnames for each of 
> > > the date time fields from each queryset are different. Is there a way 
> > > around this, should I actually be looking at a custom sql query? 
> > 
> > > On Apr 11, 12:55 am, Alex Gaynor <alex.gay...@gmail.com> wrote: 
> > > > On Fri, Apr 10, 2009 at 7:52 PM, Malcolm Tredinnick < 
> > 
> > > > malc...@pointy-stick.com> wrote: 
> > 
> > > > > On Fri, 2009-04-10 at 19:44 -0400, Alex Gaynor wrote: 
> > 
> > > > > > On Fri, Apr 10, 2009 at 7:40 PM, Malcolm Tredinnick 
> > > > > > <malc...@pointy-stick.com> wrote: 
> > > > > [...] 
> > 
> > > > > >         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. 
> > 
> > > > > It's Python and so is the heap module! Modifying things is easy. 
> I've 
> > > > > used the same solution with custom comparison functions one a 
> number of 
> > > > > occasions. Even writing a small heap implementaiton with a custom 
> > > > > comparison is only about two dozen lines. Come on, think a little 
> bit 
> > > > > outside the box! 
> > 
> > > > I'm not!  A heap sort is a great solution to this problem(but you 
> already 
> > > > knew that), it's just that the implementation in the python stdlib 
> > > doesn't 
> > > > work perfectly for this usecase(indeed I filed a ticket about this 
> > > upsteam 
> > > > several months ago, for reference it was rejected).  I did the same 
> thing 
> > > > you suggested for a blog post on this topic a number of months ago, 
> > > however 
> > > > it wasn't nearly as optimial as this(I believe you even commented to 
> that 
> > > > effect on the post). 
> > 
> > > > > 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 
> > 
> > Well, if you use some sorting mechanism that takes a key function(like 
> the 
> > one sorted takes) you could write one up that's something like: 
> > 
> > DATE_FIELD_MAPPING = { 
> >     Model1: 'date', 
> >     Model2: 'pubdate', 
> > 
> > } 
> > 
> > def my_key_func(obj): 
> >     return getattr(obj, DATE_FIELD_MAPPING[type(obj)]) 
> > 
> > And then sorted(chain(Model1.objects.all(), Model2.objects.all()), 
> > key=my_key_func) 
> > 
> > Or something similar using the heap sort method malcolm and I discussed. 
> > 
> > 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 unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/f8e4d638-30af-4be8-a797-d4ec2d67df5b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to