在 Django 中使用 taggit 时如何获取相关项目?
How to fetch related items when using taggit in django?
使用 django-taggit,我想获取与当前 post 具有相同标签的相关 post。以下是观点:
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)
我查看了文档和不同的答案(如 ),但 none 对我有用。非常感谢您的帮助。
post.tags.similar_objects()
本身将为您提供所需结果的列表(文档 here)。
使用 django-taggit,我想获取与当前 post 具有相同标签的相关 post。以下是观点:
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)
我查看了文档和不同的答案(如
post.tags.similar_objects()
本身将为您提供所需结果的列表(文档 here)。