Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Having issues displaying related items from db on templates [RSS Feed]

#1 March 10, 2008 16:01:33

[EMAIL P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Having issues displaying related items from db on templates


I'm putting together a basic feed aggregator with the following
classes in my app's model: category, feed and feeditem. Each feed is
tied to a category (ForeignKey), and each feeditem is tied to a Feed
(ForeignKey).

Here is my model:
------------
from django.db import models

class Category(models.Model):
slug = models.SlugField(prepopulate_from=('title',), help_text='This
field will prepopulate from the title field.', unique=True)
title = models.CharField(max_length=50)
description = models.TextField(help_text='A brief summary of this
category.')

class Admin:
list_display = ('slug', 'title')

class Meta:
verbose_name_plural = 'categories'

def __unicode__(self):
return self.title

def get_feeds(self):
"""
Returns the feed object for the category.
"""
return Feed.objects.filter(category=self)

def get_absolute_url(self):
return '/category/%s/' % (self.slug)

class Feed(models.Model):
title = models.CharField(max_length=200, help_text='The name of
the site providing the feed.')
feed_url = models.URLField(unique=True, help_text='The feed URL.')
public_url = models.URLField(help_text='The URL of the site
providing the feed.')
is_defunct = models.BooleanField()
category = models.ForeignKey(Category)

class Meta:
db_table = 'aggregator_feeds'

class Admin:
list_filter = ('category',)

def __unicode__(self):
return self.title

def get_feeditems(self):
"""
Returns the feeditem object for the feed.
"""
return FeedItem.objects.filter(feed=self)

def get_absolute_url(self):
return self.feed_url

class FeedItem(models.Model):
feed = models.ForeignKey(Feed)
title = models.CharField(max_length=200)
link = models.URLField()
summary = models.TextField(blank=True)
date_modified = models.DateTimeField()
guid = models.CharField(max_length=200, unique=True,
db_index=True)

class Meta:
db_table = 'aggregator_feeditems'
ordering = ("-date_modified",)

def __unicode__(self):
return self.title

def get_absolute_url(self):
return self.link
----------

I want to be able to display the FeedItems for a given category. What
would be the best way to approach this?
--~--~---------~--~----~------------~-------~--~----~
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
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#2 March 10, 2008 16:22:12

[EMAIL P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Having issues displaying related items from db on templates


I tried to add a method to the Category class like this:

def get_feed_items(self):
"""
Returns the feed items for feeds in a category.
"""
return FeedItems.objects.filter(feed__category=self)

But I get a Name Error because FeedItems aren't defined. How can I
define these when they're not defined in the model until after the
Category (and Feed)?

On Mar 10, 11:01 am, ""
<> wrote:
> I'm putting together a basic feed aggregator with the following
> classes in my app's model: category, feed and feeditem. Each feed is
> tied to a category (ForeignKey), and each feeditem is tied to a Feed
> (ForeignKey).
>
> Here is my model:
> ------------
> from django.db import models
>
> class Category(models.Model):
>         slug = models.SlugField(prepopulate_from=('title',), help_text='This
> field will prepopulate from the title field.', unique=True)
>         title = models.CharField(max_length=50)
>         description = models.TextField(help_text='A brief summary of this
> category.')
>
>         class Admin:
>                 list_display = ('slug', 'title')
>
>         class Meta:
>                 verbose_name_plural = 'categories'
>
>         def __unicode__(self):
>                 return self.title
>
>         def get_feeds(self):
>                 """
>                 Returns the feed object for the category.
>                 """
>                 return Feed.objects.filter(category=self)
>
>         def get_absolute_url(self):
>                 return '/category/%s/' % (self.slug)
>
> class Feed(models.Model):
>     title = models.CharField(max_length=200, help_text='The name of
> the site providing the feed.')
>     feed_url = models.URLField(unique=True, help_text='The feed URL.')
>     public_url = models.URLField(help_text='The URL of the site
> providing the feed.')
>     is_defunct = models.BooleanField()
>     category = models.ForeignKey(Category)
>
>     class Meta:
>         db_table = 'aggregator_feeds'
>
>     class Admin:
>         list_filter = ('category',)
>
>     def __unicode__(self):
>         return self.title
>
>     def get_feeditems(self):
>         """
>         Returns the feeditem object for the feed.
>         """
>         return FeedItem.objects.filter(feed=self)
>
>         def get_absolute_url(self):
>                 return self.feed_url
>
> class FeedItem(models.Model):
>     feed = models.ForeignKey(Feed)
>     title = models.CharField(max_length=200)
>     link = models.URLField()
>     summary = models.TextField(blank=True)
>     date_modified = models.DateTimeField()
>     guid = models.CharField(max_length=200, unique=True,
> db_index=True)
>
>     class Meta:
>         db_table = 'aggregator_feeditems'
>         ordering = ("-date_modified",)
>
>     def __unicode__(self):
>         return self.title
>
>     def get_absolute_url(self):
>         return self.link
> ----------
>
> I want to be able to display the FeedItems for a given category. What
> would be the best way to approach this?
--~--~---------~--~----~------------~-------~--~----~
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
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

  • Root
  • » Django
  • » Having issues displaying related items from db on templates [RSS Feed]

Board footer

Moderator control

Enjoy the 11th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support