Mimsy Were the Borogoves

Hacks: Articles about programming in Python, Perl, PHP, and whatever else I happen to feel like hacking at.

Django 1.0 feedgenerator and unique IDs

Jerry Stratton, September 9, 2008

I’ll have a longer post on upgrading from Django 0.96.2 to Django 1.0 later, hopefully this weekend. But here’s a note on generating RSS feeds with a unique ID. The new version of feedgenerator.py in django/utils supports adding unique_id to items; but it still doesn’t check to see if the unique ID is a permaLink; the default assumption is still that it is.

For my purpose, this is easy to fix. Line 244 and 245 are:

[toggle code]

  • if item['unique_id'] is not None:
    • handler.addQuickElement(u"guid", item['unique_id'])

I’m going to make the assumption that I’m only passing in a link once. If I’m passing a link as unique_id, I won’t pass it in as link. So if link is not None, unique_id is not a permalink:

[toggle code]

  • if item['unique_id'] is not None:
    • if item['link'] is not None:
      • handler.addQuickElement(u"guid", item['unique_id'], {u"isPermaLink": "false"})
    • else:
      • handler.addQuickElement(u"guid", item['unique_id'])

Note that link is a required parameter for SyndicationFeed.add_item.

In response to Django syndication feed guid: Django’s syndication feed makes it fairly easy to set up a feed from any object, but it uses the object’s link as the unique ID for that object. This doesn’t always work.

February 7, 2011: feedgenerator potentially improved

Hey! I was browsing my referrers today and noticed I was getting hits from Django’s bug tracker. Looks like this hack won’t be necessary to create valid RSS feeds in an unknown version after 1.3.

andreiko’s solution involves adding a new property to a Feed; the feed object will use that property to determine whether the guid provided is a permalink or not.

Here’s the sample:

[toggle code]

  • class Rss(Feed):
    • title = "Chicagocrime.org site news"
    • link = "http://chicagocrime.org/rss/"
    • description = "Updates on changes and additions to chicagocrime.org."
    • guid_is_permalink = False

Looks like a great solution. Once this issue is fixed, I won’t have to hack the Django source when new versions come out—this is the last remaining hack that I use.