简体   繁体   中英

Getting a specific tag object using django-taggit

I am using django-taggit to manage the tags in my app. 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?. 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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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