Django - 如何查询和显示多个关系结构

时间:2013-10-26 19:11:55

标签: django-models django-templates django-views

我正在构建一个RSS提要阅读器,用户可以在其中添加自己的提要和类别。我想在db中存储所有feed的内容并运行一个脚本,该脚本将定期获取db中存在的所有feed_url的内容(我有工作项目,其中每个feed都是实时获取的,这不是一个好主意)< / p>

经过多次尝试,我进入了以下模型结构。

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',)

我现在正在寻找的是能够进入视图,然后进入模板,以下实体:

- &gt;类别 - &gt; Feed - &gt;帖子

在我以前的版本中,Feed和UserFeed字段存储在一个表/模型中,我有这样的观点:

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')

这是我的模板:

{% for feed in category %}
    <p><a href="/reader/feed/{{ feed.id }} ">{{ feed.title }}</a></p>
    {% for post in feed.post_set.all|slice:"6" %}
        <p><a href="{{ post.link }}">{{ post.title }}</a></p>
        <p>{{ post.content }}</p>
        <p>{{ post.dt_published|timesince }} ago.</p>
    {% endfor %}
{% endfor %}

它按预期工作。

现在,由于我不希望在我的数据库中有多个feed_url副本,我认为新模型没问题,但我根本无法弄清楚如何查询和显示这个具有上游和下游的新结构关系。我正在查看select_related,但没有让它工作。能否请你帮忙 ?

1 个答案:

答案 0 :(得分:0)

我最终自己找到了解决方案。这有点hacky :(但是做到了。

def category(request, user_category_id):
    user = request.user
    page_title = "Category: "
    user_category = get_object_or_404(Category.objects.filter(id=user_category_id, user=user))
    user_feeds = UserFeed.objects.filter(category=user_category, user=user)
    context = {
        'page_title': page_title,
        'user_category': user_category,
        'user_feeds': user_feeds,
        }
    return expand_context_and_render(request, context, 'reader/category.html')

和模板:

{% for user_feed in user_feeds %}
    <p><strong><a href="/reader/feed/{{ feed.id }} ">{{ user_feed }}</a></strong></p>
        {% for post in user_feed.feed.post_set.all|slice:"6" %}
            <div class="col-md-4">
            <p><a href="{{ post.link }}">{{ post.title|truncatechars:"45" }}</a></p>
            <p>{{ post.content|striptags|safe|truncatechars:"85" }}</p>
            <p class="feed_date">{{ post.dt_published|timesince }} ago.</p>
            <hr />
            </div>
        {% endfor %}
{% endfor %}