Blog › Python

How to create a custom feed in Django using the Syndication Feed Framework

Hello everyone.

In this article I will show you how to create a custom feed in Django, using the Django Syndication Feed Framework 1.2 or higher.

I’ve had to create an XML with some more tags for each item in the feed.
In particular, in addition to the usual tags present in an RSS feed, I’ve added the

<short_description/> 

and

<image/>

tags.

Reading the official documentation of the framework, it is recommended to create a custom feed generator, but the process is not very clear.

Let’s see step by step how to fix everything.

First we create a custom feed generator.

This generator must create the same XML that would produce the framework’s default generator and for each item in the list it will add the new tags.

1
2
3
4
5

class CustomFeedGenerator(Rss201rev2Feed):
def add_item_elements(self, handler, item):
super(CustomFeedGenerator, self).add_item_elements(handler, item)
handler.addQuickElement(u"image", item['image'])
handler.addQuickElement(u"short_description", item['short_description'])

We see that the generator calls the method add_item_elements(handler, item) of its superclass and then adds new XML tags for each item.
Note that the values that will be inserted in the image and short_description tags must be present in the item dictionary passed as argument.

So let’s create our CustomFeed class that inherits from Feed class and see how to tell it to use the CustomFeedGenerator and how to insert the new values in the item dictionary.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

class CustomFeed(Feed):

feed_type = CustomFeedGenerator

def title(self, obj):
return u"My amazing feed"

def description(self, obj):
return u"My feed description"

def link(self, obj):
return "http://%s" % obj.get_absolute_url()

def item_extra_kwargs(self, obj):
"""
Returns an extra keyword arguments dictionary that is used with
the `add_item` call of the feed generator.
Add the 'content' field of the 'Entry' item, to be used by the custom feed generator.
"""

return { 'image': obj.get_picture().get_medium_url() if obj.get_picture() else "",
'short_description' : obj.get_preview(),}

CustomFeed is very similar to the various feeds that we are accustomed. The only difference is represented by the statement of the type of feed

feed_type = CustomFeedGenerator

and by the method

item_extra_kwargs

able to append any new values in the item dictionary, obtaining the desired result.

Find out where AI can save your company time.

One hour with our team: we map your processes, pick 2-3 concrete use cases and tell you what is worth doing first. No strings attached.

Book a free AI assessment
Subsidised finance

Incentives and grants for your software or AI project.

With specialised partners we check which incentives your project can access and prepare the paperwork and the financing plan.

  • Hyper-depreciation for 4.0 and 5.0 assets
  • National and regional grants
  • Financing plan
Book a free AI assessment