Generic Relations 10.09.2007

If you look to the right, you’ll see that there’s a new activity stream under the “Recently on nodnod” heading. It’s similar to the activity stream you’ll find on Facebook. On the backend, I’ve named it the Timeline.

I’m planning on adding some more sections to nodnod in the near future, and each section will have its own filtered timeline. On the index page, for example, the timeline will contain the events of all sections.

But in the Writing section, the timeline will only contain events that relate to the writing section.

And with Django’s generic relations and generic foreign keys, it was drop-dead easy to implement.

I have a model I named Timeline. I have it set in several of the other models’ save methods to create and add a new Timeline object whenever an add or edit occurs.

On the front end, I simply pass in the relevent Timeline objects, filtered as necessary. Then, in the template, I access the generic foreign key to fetch the correct absolute_url and related info:

 <p><a href=”{{}}”>
      {{}}</a> at {{|date:”m.d.Y” }} 
      <small><strong>{{item.action}}</strong> </small>

A big reason for the addition of the Timeline is that when those new sections are created, I don’t want to have to manually enter an announcement onto the front page that something’s changed.

Some new sections I’m considering:

  1. NaNoWriMo’s coming up, and I’ve been meaning to create a NaNoWriMo section, with word count, days, and a page that combines different days into a coherent, complete prose entry.

  2. Reviews of various things, including TV shows I watch, books, anime, games, whatever. I consume an incredible amount of media, and sometimes I want to share some of it.

In any case, I’m more and more confident in my choice to go the Django route rather than using an off-the-shelf CMS/Blog like WordPress or TextPattern. Whereas with those, I have to consider what’s available in terms of plug-ins and what not, with Django, anything I want to do, I can just go ahead and do.