How to fetch related items when using taggit in django?

只愿长相守 提交于 2020-01-06 21:11:18

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/30144220/how-to-fetch-related-items-when-using-taggit-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!