Python 查询没有命名直通字段的多个关系
我的模型中有此设置:Python 查询没有命名直通字段的多个关系,python,django,django-orm,Python,Django,Django Orm,我的模型中有此设置: class Author(models.Model): name = models.CharField(max_length=100) class Topic(models.Model): name = models.CharField(max_length=100) class Article(models.Model): name = models.CharField(max_length=100) authors = models.
class Author(models.Model):
name = models.CharField(max_length=100)
class Topic(models.Model):
name = models.CharField(max_length=100)
class Article(models.Model):
name = models.CharField(max_length=100)
authors = models.ManyToManyField(Author, null=True, blank=True)
topics = models.ManyToManyField(Topic, null=True, blank=True)
对于一位作者,我想知道他写了哪些主题:
def author_info(request, pk):
author = get_object_or_404(Author, pk=pk)
topics = ????
如果我指定了一个through字段,我可以使用它,但现在Django为我创建了through字段,因为它应该是透明的,我宁愿不引用该字段(除非有合适的Django结构)。使用:
注意:此处必须使用distinct,因为不同的文章可以选择相同的主题。使用:
注意:此处必须使用distinct,因为不同的文章可以选择相同的主题。谢谢!我自己应该能找到的,但显然我错过了。我现在就去看看,谢谢!我自己应该能找到的,但显然我错过了。我现在就去看看。
topics = Topic.objects.filter(article__authors=author).distinct()