简体   繁体   English

使用django-taggit获取特定标记对象

[英]Getting a specific tag object using django-taggit

I am using django-taggit to manage the tags in my app. 我正在使用django-taggit管理我的应用程序中的标签。 I am able to pull the tagged items like this: 我能够像这样拉出标记的项目:

photos = Photo.objects.filter(
    Q(status = 1) & Q(tags__id__in=[id])
).order_by('-position')

What I want to get is the current tag name. 我想得到的是当前的标签名称。 How can I do that? 我怎样才能做到这一点?

You are passing tags__id__in which means you know the tag ids?. 您正在传递tags__id__in ,这意味着您知道标签ID? So just get them directly. 所以直接得到它们。

tags = Tags.objects.filter(id__in=[ids])
for tag in tags:
    print tag.name

Alternately using your mentioned query (I am excluding tags__id__in from your query) 或者使用您提到的查询(我从您的查询中排除tags__id__in

photos = Photo.objects.filter(status=1).order_by('-position')
for photo in photos:
    tags = photo.tags.all()
    for tag in tags:
        print tag.name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM