Django 1.0 feedgenerator and unique IDs
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'])
-
if item['link'] is not None:
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.
-
class Rss(Feed):
- Django syndication feed framework
- “Django comes with a high-level syndication-feed-generating framework that makes creating RSS and Atom feeds easy. To create any syndication feed, all you have to do is write a short Python class. You can create as many feeds as you want.”
More Django
- Django: fix_ampersands and abbreviations
- The fix_ampersands filter will miss some cases where ampersands need to be replaced.
- Custom managers for Django ForeignKeys
- I’ve got one really annoying model for keywords. There’s one category of keywords that, by default, should not show up when used as a ForeignKey for most models. Key word: most.
- Fixing Django 1.2.4’s SuspiciousOperation on filtering
- When you get the message “Filtering by keyword not allowed” in Django 1.2.4, here’s one way to fix it.
- Reusing Django’s filter_horizontal
- Just as with pop-ups, it’s possible to use the built-in JavaScript for filtering multiple-selection popups on custom forms.
- Django formsets and date/time fields
- Date/Time fields in Django formsets appear to have incompatible default values, resulting in forms using them always looking as though they’ve got a new entry when they don’t.
- 25 more pages with the topic Django, and other related pages
