title: Django - How to query and display multiple relationship struture
body: <p>I'm building a RSS feed reader where users can add own feeds and categories. I want to store content all of the the feeds in db and run a script that would fetch that content for all of the feed_url present in db periodically (I had working project where each feed is fetched live and it wasn't good idea)</p>
  
  <p>Now after several attempts I got into the following model structure. </p>
  
  <pre><code>class Category(models.Model):
      name = models.CharField(unique=False, max_length=64)
      user = models.ForeignKey(User)
  
      def __unicode__(self):
          return self.name
  
      class Meta:
          ordering = ('name',)
  
  
  class Feed(models.Model):
      feed_url = models.URLField(unique=True)
  
      def __unicode__(self):
          return self.feed_url
  
  
  class UserFeed(models.Model):
      feed = models.ForeignKey(Feed)
      title = models.CharField(max_length=256)
      category = models.ForeignKey(Category)
      user = models.ForeignKey(User)
  
      def __unicode__(self):
          return self.title
  
  
  class Post(models.Model):
      feed = models.ForeignKey(Feed, related_name='feed_posts')
      ## starting from here, my cronjob / script populates the data (using feedparser lib)
      title = models.CharField(max_length=256)
      content = models.TextField()
      link = models.URLField(max_length=512)
      dt_published = models.DateTimeField()
      dt_cached = models.DateTimeField(auto_now_add=True)
  
  
      def __unicode__(self):
          return '%s (%s)' % (self.feed, self.title)
  
      class Meta:
          ordering = ('-dt_published',)
  </code></pre>
  
  <p>What I'm looking for now is ability for pull into a view and later into templates, the following entities:</p>
  
  <p>->Category -> Feed -> Posts </p>
  
  <p>In my previous version where Feed and UserFeed fields were stored in one table/model, I had such view: </p>
  
  <pre><code>def category(request, category_id):
      user = request.user
      page_title = "Category: "
      category = get_object_or_404(Category.objects.filter(id=category_id, user=user)).feed_set.all()
      category_name = Category.objects.get(id=category_id)
      context = {
          'page_title': page_title,
          'category': category,
          'category_name': category_name,
          }
      return expand_context_and_render(request, context, 'reader/category.html')
  </code></pre>
  
  <p>and this is my template:</p>
  
  <pre><code>{% for feed in category %}
      &lt;p&gt;&lt;a href="/reader/feed/{{ feed.id }} "&gt;{{ feed.title }}&lt;/a&gt;&lt;/p&gt;
      {% for post in feed.post_set.all|slice:"6" %}
          &lt;p&gt;&lt;a href="{{ post.link }}"&gt;{{ post.title }}&lt;/a&gt;&lt;/p&gt;
          &lt;p&gt;{{ post.content }}&lt;/p&gt;
          &lt;p&gt;{{ post.dt_published|timesince }} ago.&lt;/p&gt;
      {% endfor %}
  {% endfor %}
  </code></pre>
  
  <p>And it was working as intended.</p>
  
  <p>Now, since I don't want to have multiple copies of feed_url in my database, I think the new models are OK, but I simply can't figure out how to query and display this new structure which has upstream and downstream relationships. I was looking at select_related but didn't get it to work. Could you please help ? </p>