views:

31

answers:

1

I'm using model inheritance to manage a multiple models queryset:

from django.db import models
from django.contrib.sites.models import Site
from django.contrib.auth.models import User
from imagekit.models import ImageModel
import datetime

class Entry(models.Model):
    date_pub = models.DateTimeField(default=datetime.datetime.now)
    author = models.ForeignKey(User)
    via = models.URLField(blank=True)
    comments_allowed = models.BooleanField(default=True)

    class IKOptions:
        spec_module = 'journal.icon_specs'
        cache_dir = 'icon/resized'
        image_field = 'icon'


class Post(Entry):  
    title = models.CharField(max_length=200)
    description = models.TextField()
    slug = models.SlugField(unique=True)

    def __unicode__(self):
        return self.title


class Photo(Entry):
    alt = models.CharField(max_length=200)
    description = models.TextField(blank=True)
    original = models.ImageField(upload_to='photo/')    

    def __unicode__(self):
        return self.alt


class Quote(Entry):
    blockquote = models.TextField()
    cite = models.TextField(blank=True)

    def __unicode__(self):
        return self.blockquote

A conditional template is enough to render the right snippets of html with a view based on Entry.objects.all():

{% extends "base.html" %}
{% block main %}
<hr>
{% for entry in entries %}

{% if entry.post %}
[...do something with entry.post]
{% endif %}

{% if entry.photo %}
[...do something with entry.photo]
{% endif %}

[...and so on]

Now I'm trying to generate a RSS feed using the new Feed Framework featured in Django 1.2 but without any luck... The over-simplified settings of the framework don't let me specify conditional *item_title* and *item_description* based on the child objects of Entry:

from django.contrib.syndication.views import Feed
from totanus.journal.models import Entry, Photo, Post, Quote


class LatestEntriesFeed(Feed):
    title = "RSS Feed"
    link = "/journal/"
    description = "Journal RSS"

    def items(self):
           return Entry.objects.order_by('-date_pub')[:10]

    def item_title(self, item):
           # if child is Post
           return item.post.title # this isn't working, of course...

Should I create a custom view-template set to manage the RSS creation and syndication or is there a way to use the Feed framework with this subclassed model?

+2  A: 

You have two options:

  1. use the title_template and description_template attributes on the Feed class to point to templates that can handle a very generic input variable.

  2. Recreate your template logic in the feed class' methods so that your template variable gets a normalized piece of data.

Either one will get you to the same end point.

Gabriel Hurley
Thank you Gabriel. Option 1) worked just well.Actually I saw the docs but I misunderstood the point.About option 2) - to be honest - I don't think I completely understand what you meant. However... Thank you a lot!
Matteo