I'm working on a blog/CMS type application where I use a number of
different models for different content types.  Each of the content
types (article, book review, film review, etc.) is sub-classed from a
central "Node" model.  What I'd like to be able to do is display a
list of all nodes on a page, blog-style, such that the details of each
node are shown.  Unfortunately, I can't think of a good way to do
this, because each sub-class has different fields.

To make this a bit more concrete, here are the models in question:

------------------------------------------------------------------

class Section(models.Model):
  section = models.CharField(max_length=50)
  keyword = models.CharField(max_length=15)

  class Admin:
    pass

class Archetype(models.Model):
  classname = models.CharField(max_length=15)

  class Admin:
    pass

class Node(models.Model):
  author = models.ForeignKey(User)
  created = models.DateTimeField()
  published = models.BooleanField()
  promoted = models.BooleanField()
  section = models.ForeignKey(Section)
  archetype = models.ForeignKey(Archetype)
  title = models.CharField(max_length=255)
  slug = models.SlugField(max_length=25, prepopulate_from=("title",))

  def _blurb(self):
    if self.archetype.classname is not "Node":
      return getattr(self, self.archetype.classname.lower()).blurb
    else:
      return u"Created by %s" % (self.author.username,)

  blurb = property(_blurb)

  class Admin:
    pass

class Article(Node):
  body = models.TextField()
  blurb = property(lambda self: blurbify(self.body, 100))

  class Admin:
    pass

class BookReview(Node):
  book_author = models.CharField(max_length=255)
  startdate = models.DateField()
  enddate = models.DateField()
  url = models.URLField()
  body = models.TextField()

  blurb = property(lambda self: blurbify(self.body, 100))

  class Admin:
    pass

class FilmReview(Node):
  viewdate = models.DateField()
  releasedate = models.DateField()
  url = models.URLField()
  rating = models.CharField(max_length = 2)
  body = models.TextField()

  blurb = property(lambda self: blurbify(self.body, 100))

  class Admin:
    pass

------------------------------------------------------------------

I've removed some member functions to make it a little simpler.

You'll notice the "blurb" attribute that Node and each of the sub-
classes implements.  Originally I had only wanted to display a
shortened blurb, rather than the entire post, so this was an easy way
to do that.  But now that I want to display the entire post for each
node, I don't know what to do.

One thing I thought of would be to do something similar to the blurb
attribute, having some sort of "display" attribute that each sub-class
would implement, which would return a formatted string that I could
then just throw up in the template.  The problem with this approach is
that it's not very MVC-ish--I'm essentially putting template code into
the model.

What would be better is if I could call a separate template for each
item in the list, based on the Archetype or whatever, but I don't know
if there's any way to do that in Django.  Any thoughts?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to