Multi-model RSS Feed in Django
I am trying to figure out how to have a multi-model RSS feed in Django. That is, I would like Django to serve updates of two different models under one RSS url.
Here is what I have thus far
class ArticleFeed(Feed):
title = "Articles"
link = "/articles/"
description = "RSS feed of Articles"
def items(self):
return Article.objects.filter(is_published=1)
def item_title(self, item):
return item.title
def item_description(self, item):
return item.get_description()
def item_link(self, item):
return item.get_absolute_url()
class VideoFeed(Feed):
pass
class atomFeed(Feed):
feed_type = Atom1Feed
In urls.py I have the following path
path('rss/', ArticleFeed(), name="article_feed"),
I would like rss/
to provide updates to both the Article and Video model. The Video model has similar fields but is non-verbatim.
you can use queryset3 = queryset1.union(queryset2) to concatenate items of two different models and use this in feed.items(self):
def items(self):
query1 = Article1.objects.filter(is_published=1)
query2 = Article2.objects.filter(is_published=1)
query = query1.union(query2)
# do any sorting AFTER union
query_sorted = query.order_by('a_field', 'another_field')
self.item_count = query_sorted.count()
return query_sorted
union() works as long as queries have the same fields.
note: above is "pseudo" code - I did not run it so it might have syntax errors