通过外键快速获取与其他 2 个对象相关的对象查询集

Quickly get query set of objects related to 2 other objects by a Foreign key

这是我的模型:

class A(models.Model):
  #Some fields

class B(models.Model):
  #Some fields

class C(models.Model):
  a = models.ForeignKey(A)
  b = models.ForeignKey(B)
  #Some more fields

  class Meta:
     unique_together = ['a', 'b']

Now based on some object of A (say 'a') , I want set of all 'b' for which C.a = 'a' and C.b = 'b' .
In other words, get a set of objects of B for which entries exist in a.c_set

目前我这样做的方式是:

qset_c = C.objects.filter(a='a')    
qset_b = [c.b for c in qset_c]

首先这给了我一个列表(最好是查询集)。
其次,我担心第 2 行具有 list comprehension ,因为它可能需要大量时间处理大量数据。

有没有更好的方法来实现这个?

注:我明白了,表述有点不清楚。如果有人可以编辑它使它更清楚,那将有很大帮助

这会如你所愿吗?

set_c = C.objects.filter(a=a).values_list('b', flat=True)