简体   繁体   English

使用django queryset访问不同的外键关系

[英]Accessing distinct foreign key relationships with django queryset

Given basic book/author models: 鉴于基本书/作者模型:

class Book(models.Model):
    title = models.CharField(max_length=200)
    author = models.ForeignKey('Author')
    publisher = models.ForeignKey('Publisher')

class Author(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)

Lets say I have a very complex query to select books based on a bunch of parameters (my actual models are much more complex than these). 假设我有一个非常复杂的查询来根据一堆参数选择书籍(我的实际模型比这些更复杂)。 For simplification sake, my complex query for this example will just select books that have the word "the" in their title: 为简化起见,我对此示例的复杂查询只会选择标题中包含“the”一词的书籍:

q = Book.objects.filter(title__icontains="the")

Is there a way (other than looping, or using reverse foreign key lookups) to get all distinct Author objects linked to the books in this query? 有没有办法(除了循环或使用反向外键查找)来获取链接到此查询中的书籍的所有不同的Author对象? Eg I have tried adding: 例如,我尝试添加:

q = q.values('author').distinct()

But this simply returns the author__id values . 但这只是返回author__id values I am trying to go about it this way, as my 'complex query' is quite time/resource intensive, and I'd only like to run that query once (to get the books, and separate a list of distinct authors). 我试图这样做,因为我的“复杂查询”非常耗费时间/资源,而且我只想运行一次查询(获取书籍,并分离不同作者的列表)。 Also the data sets need to be flattened (a separate list of Books and Authors) as it is getting presented via django rest framework, and my clients require the data sets to be flat. 此外,数据集需要展平(书籍和作者的单独列表),因为它通过django rest框架呈现,我的客户要求数据集是平的。 Eg: 例如:

{
  "books": [
      {"id": 1, "title": "The Book.", "author": 1, "publisher": 1},
      {"id": 2, "title": "The Other Book.", "author": 2, "publisher": 1},
      {"id": 3, "title": "The Best Book.", "author": 1, "publisher": 1},
    ],
  "authors": [
      {"id": 1, "first_name": "Joe", "last_name": "Smith"},
      {"id": 2, "first_name": "Gina", "last_name": "Randolph"}
    ]
}

Alternatively, is there a simple way with django rest framework to take the results of a complex query with nested foreign keys, and flatten it out? 或者,有一个简单的方法,使用django rest框架来获取带有嵌套外键的复杂查询的结果,并将其展平?

Really, the best way to get the authors is to query for authors rather than books. 实际上,获得作者的最佳方式是查询作者而不是书籍。 You can follow the relationship via the double-underscore syntax: 您可以通过双下划线语法来关注关系:

q = Author.objects.filter(book__title__icontains="the")

But since you say your query is intensive and you would prefer to run it once, an alternative would be to use select_related to do a JOIN and then manually process the results to get the authors: 但是既然你说你的查询是密集的并且你更喜欢运行一次,那么另一种方法是使用select_related来进行JOIN,然后手动处理结果以获得作者:

q = Book.objects.select_related('author').filter(title__icontains="the")
authors = set(book.author for book in q)

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

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