[英]In django-taggit, how to get tags for objects that are associated with a specific user?
I have a series of objects that are associated with specific users, like this: 我有一系列与特定用户相关联的对象,如下所示:
from django.db import models
from django.contrib.auth.models import User
from taggit.managers import TaggableManager
class LibraryObject(models.Model):
title = models.CharField(max_length=255)
owner = models.ForeignKey(User)
tags = TaggableManager()
class Meta:
abstract = True
class Book(LibraryObject):
summary = models.TextField()
class JournalArticle(LibraryObject):
excerpt = models.TextField()
# ...etc.
I know that I can retrieve all tags like this: 我知道我可以像这样检索所有标签:
>>> from taggit.models import Tag
>>> Tag.objects.all()
But how can I retrieve all tags that are associated with a specific user? 但是,如何检索与特定用户关联的所有标记? I'm imagining something like
Tag.objects.filter(owner=me)
, but of course that doesn't work. 我想象像
Tag.objects.filter(owner=me)
,但当然这不起作用。
For reference, here's the django-taggit documentation . 作为参考,这是django-taggit文档 。
I've came across a similar problem, and here is my solution: 我遇到过类似的问题,这是我的解决方案:
tags = Tag.objects.filter(book__owner=me)
tags |= Tag.objects.filter(journalarticle__owner=me)
tags = tags.distinct()
hope it will help~ 希望它会有所帮助〜
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.