在django中使用taggit时如何获取相关的项目? [英] How to fetch related items when using taggit in django?

查看:183
本文介绍了在django中使用taggit时如何获取相关的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 django-taggit ,我想要抓取相关的帖子与当前帖子具有相同的标签。以下是这些视图:

Using django-taggit, I'd like to fetch related posts which have the same tag(s) as the current post. Here is the views:

from taggit.managers import TaggableManager, TaggedItem
from taggit.models import Tag

def post(request, post_slug):

    post = Article.objects.get(slug = post_slug)
    comments = Comment.objects.filter(post=post)
    #tag = get_object_or_404(Tag, id= post.id)
    #related = Article.objects.filter(tags= post.tags.similar_objects()) 

    print "RELATED \n"   
    #print related

    d = dict(post=post, comments=comments, form=CommentForm(), 
             user=request.user)
    d.update(csrf(request))
    return render_to_response("article/post.html", d)

我查看了文档和不同的答案(如),但对我来说没有效果。感谢您的帮助。

I looked at the docs and different answers (like this) but none worked for me. So appreciate your help.

推荐答案

post.tags.similar_objects()本身将为您提供所需结果列表(文档 here )。

post.tags.similar_objects() in and of itself will provide you with a list of the results you want (documentation here).

这篇关于在django中使用taggit时如何获取相关的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆