【问题标题】:How to fetch related items when using taggit in django?在 django 中使用 taggit 时如何获取相关项目?
【发布时间】:2015-05-09 19:37:47
【问题描述】:

使用django-taggit,我想获取与当前帖子具有相同标签的相关帖子。以下是观点:

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)

我查看了文档和不同的答案(例如 this),但没有一个对我有用。非常感谢您的帮助。

【问题讨论】:

  • post.tags.similar_objects() 本身不提供包含您想要的结果的列表吗? (见documentation here
  • @LaundroMat 是的,确实如此!请回答,我会接受。

标签: django django-taggit


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2017-09-28
    • 1970-01-01
    • 2019-06-06
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 2012-08-22
    • 1970-01-01
    • 2018-11-06
    相关资源
    最近更新 更多