0

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.

Community
  • 1
  • 1
Jand
  • 2,527
  • 12
  • 36
  • 66
  • Doesn't `post.tags.similar_objects()` in and of itself provide a list with the results you desire? (see [documentation here](http://django-taggit.readthedocs.org/en/latest/api.html#TaggableManager.similar_objects)) – Mathieu Dhondt May 09 '15 at 20:23
  • @LaundroMat Yes, it does! Please answer and I'll accept. – Jand May 09 '15 at 20:30

1 Answers1

2

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

Mathieu Dhondt
  • 8,405
  • 5
  • 37
  • 58