Model inheritance and RSS Feed framework
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_f开发者_如何学JAVAield = '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?
You have two options:
use the
title_template
anddescription_template
attributes on the Feed class to point to templates that can handle a very generic input variable.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.
精彩评论