2014-01-18 81 views
0

我正在研究多用戶rss閱讀器。我想限制帖子的顯示只限於未讀的帖子。我已經設法在我的單個「feed」視圖中做到這一點,但我無法弄清楚如何在多個feed中也做同樣的「category」視圖。視圖和模板中的Django模型關係

我一直在試圖像這裏https://docs.djangoproject.com/en/1.5/topics/db/queries/#spanning-multi-valued-relationships但對我來說

我應該改變我的「類別」視圖或代碼模板代碼沒有奏效?如果是的話,你會怎麼做呢?

謝謝! -S

車型

class UserCategory(models.Model): 
    name = models.CharField(unique=False, max_length=64) 
    user = models.ForeignKey(User) 
    slug = AutoSlugField(populate_from='name', always_update='True', unique_with='user') 

class Feed(models.Model): 
    feed_url = models.URLField(unique=True) 
    default_title = models.CharField(max_length=64, blank=True) 
    link = models.URLField(blank=True) 

class UserFeed(models.Model): 
    feed = models.ForeignKey(Feed) 
    title = models.CharField(max_length=64) 
    category = models.ForeignKey(UserCategory) 
    user = models.ForeignKey(User) 
    slug = AutoSlugField(populate_from='title', always_update='True', unique_with='user') 

class Post(models.Model): 
    feed = models.ForeignKey(Feed) 
    title = models.CharField(max_length=256) 
    content = models.TextField() 
    link = models.URLField(max_length=512) 

class ReadPost(models.Model): 
    user = models.ForeignKey(User) 
    post = models.ForeignKey(Post) 

意見

def feed(request, user_feed_slug): 
    user_feed = get_object_or_404(UserFeed.objects.filter(slug=user_feed_slug, user=request.user)) 
    read_post = ReadPost.objects.filter(user=request.user).values_list('post') 
    posts = Post.objects.select_related().filter(feed=user_feed.feed).exclude(id__in=read_post) 

def category(request, user_category_slug): 
    user_category = get_object_or_404(UserCategory.objects.filter(slug=user_category_slug, user=request.user)) 

模板

feed 
    {% for post in posts %} 
     {{ post.title }}      
    {% endfor %} 

category 
{% for feed in user_category.userfeed_set.all %} 
    {{ feed.title }} 
    {% for post in feed.feed.post_set.all %} 
     {{ post.title }} 
     {{ post.content }} 
    {% endfor %} 
{% endfor %} 

回答

1

你可以寫custom template filter,即:

@register.filter 
def unread(posts, read_posts): 
    return posts.exclude(id__in=read_posts) 

(在您必須將read_post傳遞到類別模板上下文之前)。

+0

我不熟悉自定義過濾器。我不確定如何正確地實現它?我應該把它放在這裏:{%for feed in user_category.userfeed_set.all | unread%}或這裏{%for feed in feed.feed.post_set.all | unread%} ??還應該加上一些額外的參數,如未讀:帖子?我正在嘗試所有這些選項,但它似乎沒有工作:( – sikor

+1

您必須將此過濾器應用於發佈queryset並另外傳遞用戶閱讀帖子。因此,在此示例中,它可能是這樣的:'{%for post在feed.feed.post_set.all | unread:read_posts%}'。 –

+0

經過進一步調整就像一個魅力!Dzieki :) – sikor

0

試試這個查詢集:

def category(request, user_category_slug): 
    user_category = get_object_or_404(UserCategory, slug=user_category_slug, 
              user=request.user)) 

    feeds = UserFeed.objects.filter(category__slug=user_category_slug, user=request.user)\ 
       .prefetch_related('feed__post_set') 

然後在你的模板:

{% for feed in feeds %} 
    {{ feed.title }} 
    {% for post in feed.feed.post_set.all %} 
     {{ post.title }} 
     {{ post.content }} 
    {% endfor %} 
{% endfor %} 
+0

我試過了,但是這並沒有排除那些被讀取的文章(那些存儲在ReadPost模型中的文件,你會有其他想法嗎? – sikor