Creating dynamic RSS feed with parameters

Quick look at setting up RSS feed that takes a parameter.

Published: Sept. 27, 2021
App Store

Setting up basic RSS feed with Django is pretty quick process thanks to Syndication framework included in Django. And it turns out setting up RSS feed that takes a parameter is not significantly more involved.

For my IndieAppsCatalog I wanted to generate separate RSS feeds for all categories, so people could follow just "Productivity" category, for example.

Let's see how this is done, with emphasis on what is different from setting up basic RSS.

Setting up the feed

You need a way to identify the "base object" the RSS feeds uses. For that we can use get_object method:

class NewlyAddedAppsByCategory(Feed):
    author_name = "Indie Apps Catalog"

    def get_object(self, request, category_id, **kwargs):
        return AppCategory.objects.get(appstore_id=category_id)

This is the key difference from basic RSS feeds which don't make use of the get_object method.

Next you can change properties of the feed like title, link or description to be methods which allows you to customize them based on the category. For example:

def title(self, obj):
    return "IndieAppsCatalog: Newly added apps to '{0}'".format(obj.display_name)

def link(self, obj):
    return obj.get_absolute_url()

The obj is AppCategory instance so we can use its display_name property to customize title for this RSS feed.

Customizing what items get returned

The last different piece to setup in this class is the items method.

def items(self, obj):
    return AppRecord.all().filter(primary_category=obj)

Since we got the category as obj we can use it with the filter method. And that are all the different steps needed to have RSS feed with parameter. Let's look at the URL setup.

Setting up the URL

Feed URLs work like any other URL in Django, but for the sake of completeness here it is:

urlpatterns += [
    path('feed/newly-added/<int:category_id>', NewlyAddedAppsByCategory()),
]

The category_id parameter will get passed to the get_object method we looked at in the beginning.

You can also visit official docs for more details.

Bluesky logo

Follow on Bluesky to not miss new posts

Filip Němeček profile photo

WRITTEN BY

Filip Němeček Mastodon

iOS blogger and developer with interest in Python/Django.

iOS blogger and developer with interest in Python/Django.