On Mar 10, 2009, at 12:43 AM, bax...@gretschpages.com wrote:
> > I'm trying to build a combined feed, and not having much luck. Here's > what I have: > > class CombinedFeed(Feed): > title = site.name +" combined feed" > link = "/" > description = "Latest updates from all "+site.name +" feeds" > description_template = 'feeds/combined.html' > > def items(self): > combined = [] > articles = Article.objects.all().order_by('-created')[: > 40] > events = > Event.objects.filter(start_date__gte=now).order_by > ('start_date')[:50] > news = > News_item.objects.filter(pub_date__lte=now).order_by('- > pub_date')[:20] > combined = itertools.chain(news, articles, projects, > issues, > events) > for item in combined: > try: > item.date = item.created > except: > try: > item.date = item.topic_modification_date > except: > try: > item.date = item.pub_date > except: > try: > item.date = item.start_date > except: > item.date = now > return combined Itertools.chain returns a generator. Once you've run through the generator a single time (as you're doing with "for item in combined") it's exhausted: your return statement is returning an empty iterable. Here's how I do something similar with one of my feeds: def items(self): el = list(Entry.objects.open()[:10]) el .extend(list(NewsLink.objects.filter(post_date__gte=el[-1].disp_date))) el.sort(key=lambda x: getattr(x, x._meta.ordering[0].strip('-')), reverse=True) return el I've just decided that there aren't enough items to fret about calling list() on the whole thing, and it's important to me that everything return in proper order. If you're worried about prematurely evaluating the querysets (though you're doing that right now!), I remember once seeing a recipe on activestate or somewhere for a iter-style function that took several iterables and then returned items from each iterable in sorted order. Also, you can avoid the whole attribute-testing chain (which would allow you to return an un-evaluated QuerySet), by using the _meta.ordering[0] trick to get the proper attribute name. Or specify the proper date attribute using a python-only attribute on each model. I do something similar to specify different templates to use for different models when rendering the feed: each model has a non-db 'feed_template' string attribute. Hope something in there is useful, Eric > > > Which doesn't appear to do anything. No error, but no ojb in > combined.html, either. > Suggestions? > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---